uva1069 - Always an integer

UVA - 1069:Always an integer UVA - 1069:Always an integer 来源:UVA 标签:字符串,数论 参考资料: 相似题目: 题目 输入 输出 输入样例 (n^2-n)/2 (2n^3+3n^2+n)/6 (-n^14-11n+1)/3 . 输出样例 Case 1: Always an ... 阅读详情

Combinatorics is a branch of mathematics chiefly concerned with counting discrete objects. For instance, how many ways can you pick two people out of a crowd of n people? Into how many regions can you divide a circular disk by connecting n points on its boundary with one another? How many cubes are in a pyramid with square layers ranging from 1×1 to n×n cubes?

\epsfbox{p4119.eps}

Many questions like these have answers that can be reduced to simple polynomials in n. The answer to the first question above is n(n - 1)/2, or (n$\scriptstyle \wedge$2 - n)/2. The answer to the second is (n$\scriptstyle \wedge$4 - 6n$\scriptstyle \wedge$3 + 23n$\scriptstyle \wedge$2 - 18n + 24)/24. The answer to the third is n(n + 1)(2n + 1)/6, or (2n$\scriptstyle \wedge$3 + 3n$\scriptstyle \wedge$2 + n)/6. We write these polynomials in a standard form, as a polynomial with integer coefficients divided by a positive integer denominator.

These polynomials are answers to questions that can have integer answers only. But since they have fractional coefficients, they look as if they could produce non-integer results! Of course, evaluating these particular polynomials on a positive integer always results in an integer. For other polynomials of similar form, this is not necessarily true. It can be hard to tell the two cases apart. So that, naturally, is your task.

Input 

The input consists of multiple test cases, each on a separate line. Each test case is an expression in the form (P)/D, where P is a polynomial with integer coefficients and D is a positive integer denominator. P is a sum of terms of the form Cn$\scriptstyle \wedge$E, where the coefficient C and the exponent E satisfy the following conditions:

  1. E is an integer satisfying 0$ \le$E$ \le$100. If E is 0, then Cn$\scriptstyle \wedge$E is expressed as C. If E is 1, then Cn$\scriptstyle \wedge$E is expressed as Cn, unless C is 1 or -1. In those instances, Cn$\scriptstyle \wedge$E is expressed as n or - n.
  2. C is an integer. If C is 1 or -1 and E is not 0 or 1, then the Cn$\scriptstyle \wedge$E will appear as n$\scriptstyle \wedge$E or - n$\scriptstyle \wedge$E.
  3. Only non-negative C values that are not part of the first term in the polynomial are preceded by +.
  4. Exponents in consecutive terms are strictly decreasing.
  5. C and D fit in a 32-bit signed integer.

See the sample input for details.

Input is terminated by a line containing a single period.

Output 

For each test case, print the case number (starting with 1). Then print `Always an integer' if the test case polynomial evaluates to an integer for every positive integer n. Print `Not always an integer' otherwise. Print the output for separate test cases on separate lines. Your output should follow the same format as the sample output.

Sample Input 

(n^2-n)/2 
(2n^3+3n^2+n)/6 
(-n^14-11n+1)/3 
.

Sample Output 

Case 1: Always an integer 
Case 2: Always an integer 
Case 3: Not always an integer

  给你一个多项式,问n取任何值的时候这个多项式的值是否都是整数。

  设最高项数为k。当k=0时,不存在n,只要计算P(1)是否为整数。当k=1时,设P(n)=a*n+b,P(n+1)-P(n)=a,是等差数列,只要首项和公差都是D的倍数,n取任何值P都是d的倍数,也就是只要P(1)和P(2)是D的倍数就行。当k=3时,设P(n)=a*n^2+b*n+c,P(n+1)-P(n)=2an+a+b,因为2an+a+b是n的一次多项式,只有当n=1和n=2时都是D的倍数的时候2an+a+b才能被D整除,也就是P(1),P(2)-P(1),P(3)-P(2)都要是D的倍数,因此只要满足P(1),P(2),P(3)都是D的倍数就行。

  根据数学归纳法,只要n的取值从1...k+1的P(n)都是D的倍数,n取任何值都会是D的倍数。

  训练指南上还有个差分数列的图,写的非常清楚。。

  我用数据结构里面那个表达式求值的方法写的,代码略坑,还有就是昨天数据结构考的跟翔一样哭

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define eps 1e-9
#define MAXN 2010
#define MOD 1000000007
using namespace std;
int isp[100],icp[100],L,D;  //isp栈内优先级 icp栈外优先级
char str[MAXN],s[MAXN];
void init(){
    isp['#']=0;
    isp['(']=1;
    isp['*']=isp['/']=5;
    isp['+']=isp['-']=3;
    isp['^']=7;
    isp[')']=8;
    icp['#']=0;
    icp['(']=8;
    icp['*']=icp['/']=4;
    icp['+']=icp['-']=2;
    icp['^']=6;
    icp[')']=1;
}
long long cal(long long x1,long long x2,char op){
    switch(op){
    case('+'):return (x1+x2)%D;
    case('-'):return (x1-x2)%D;
    case('*'):return (x1*x2)%D;
    case('^'):
        long long ret=1;
        while(x2--) ret=(ret*x1)%D;
        return ret;
    }
}
int check(int x){
    stack<char> st;
    stack<long long> st2;
    st.push('#');
    st2.push(0);
    int i=0;
    while(str[i]!='/'){
        if(str[i]=='n'){
            st2.push(x);
            i++;
        }
        if(isdigit(str[i])){
            long long m=0;
            while(isdigit(str[i])){
                m=m*10+str[i]-'0';
                i++;
            }
            st2.push(m);
        }
        char op=st.top();
        if(icp[str[i]]>isp[op]){
            st.push(str[i]);
            i++;
        }
        else if(icp[str[i]]<isp[op]){
            long long num1=st2.top();
            st2.pop();
            long long num2=st2.top();
            st2.pop();
            long long t=cal(num2,num1,op);
            st.pop();
            st2.push(t);
        }
        else{
            st.pop();
            i++;
        }
    }
    if(st2.top()%D==0) return 1;
    return 0;
}
int main(){
    freopen("in.txt","r",stdin);
    init();
    int cas=0;
    while(scanf("%s",s),s[0]!='.'){
        //加上最开始的+(如果有)和*
        int k=0;
        for(int i=0;s[i];i++){
            str[k++]=s[i];
            if(isdigit(s[i])&&s[i+1]=='n') str[k++]='*';
            if(s[i]=='('&&isdigit(s[i+1])) str[k++]='+';
        }
        str[k]=0;
        L=strlen(str);
        //最高项
        int K=1;
        for(int i=0;i<L;i++) if(str[i]=='^'){
            int j=i+1,m=0;
            while(isdigit(str[j])){
                m=m*10+str[j]-'0';
                j++;
            }
            K=m+1;
            break;
        }
        //算出除数
        for(int i=0;i<L;i++) if(str[i]=='/'){
            int j=i+1,m=0;
            while(isdigit(str[j])){
                m=m*10+str[j]-'0';
                j++;
            }
            D=m;
            break;
        }
        int flag=1;
        for(int i=1;i<=K;i++) if(!check(i)){
            flag=0;
            break;
        }
        printf("Case %d: ",++cas);
        if(flag) printf("Always an integer\n");
        else printf("Not always an integer\n");
    }
    return 0;
}



Flink on k8s 环境搭建(一) Flink on Yarn的环境搭建过程中,需要进行配置较多,且需要搭建zookeeper Hadoop Yarn 等相关组件,安装流程比较复杂,集群出现问题重新安装的流程也比较复杂,且Yarn的3个节点中 只能起了 3个resourceManager和1个NodeManager,Flink 作业申请资源时只能 向NodeManager的节点申请资源,整体有资源瓶颈的隐患(后继flink作业会越来越多),现在尝试进行Flink on k8s 的环境搭建。 阅读详情

相关推荐

如何在R中高效安装ropls及其依赖包

本文详细介绍了在R语言中高效安装ropls包及其依赖的完整流程。作为Bioconductor生物信息学分析包,ropls的安装需通过BiocManager管理器进行。文章提供了标准安装步骤、跨系统避坑指南及安装后验证方法,帮助用户解决R版本、编译环境和网络镜像等常见问题,确保代谢组学数据分析工具顺利部署。

efc123456的博客 920

Always an integer UVALive - 4119 总是整数

题目链接 组合数学主要研究计数问题。比如,从n 个人中选两个人有多少种方法?圆周上有n个点,两两相连之后最多能把圆面分成多少部分?如图2-16 所示。有一个金字塔,从塔顶开始每一层分别有1 x 1 , 2x2, ..., nXn 个小立方体,问一共有多少个小立方体? 很多问题的答案都可以写成n 的简单多项式。比如上述第一个问题的答案是n(n一1)/2 ,也就是(n^2 -n)/2 ; 第二个问...

Nicolas的博客 427

CCS环境下ADC采样波形的实时可视化调试技巧

本文详细介绍了在CCS环境下实现ADC采样波形实时可视化的调试技巧。通过数组缓存优化、多通道同步显示和波形测量工具的高级用法,帮助开发者高效调试嵌入式系统中的ADC采样问题,提升信号分析的准确性和效率。

weixin_33712987的博客 428

Denso Create Programming Contest 2022 (AtCoder Beginner Contest 239) C~E 题解

ABC239/Denso2022 C~E 题解 C - Knight Fork D - Prime Sum Game E - Subtree K-th Max

天生我材必有用,千金散尽还复来。 8442

卫星测试科普(三):卫星载荷的核心测试设备探秘

总的说来,卫星芯片与元器件,部组件与单机,整机与子系统,系统与整星的设计、定型、制造、验证、总装、发射、在轨等环节都离不开各种测试场景,对于这些卫星测试场景而言,“射频三大件”与高速数字示波器是构成强大测试系统的基础。卫星通信信号生成:覆盖Ka、Q等波段,具备扫频、脉冲、数字调制、噪声信号、雷达信号、数据链信号、OFDM 等多种信号库,用于测试和验证卫星电子设备的电气性能、频率响应、抗干扰能力等。卫星的通信需要高效、快速地传输大量数据,高速信号的质量至关重要,如果卫星信号不佳,卫星就会出现故障或者隐患。

spwbjss的博客 2340

STM32G070xx将Flash页分块方式存储,固定数据块存储,实现一次擦除多次写入

本例程使用STM32G070xx芯片,Flash大小128KB,Flash每页(2048字节)写满后再擦除。操作存储区分为64块,每块32字节,即写满64次擦除一次;标定存储区分为16块,每块128字节,即写满16次擦除一次,每个存储区的块都有各自的头部标记

RaphaelCeo的博客 1556

UVA 1069 - Always an integer(数论)

1069 - Always an integer

Remilia's 1693

2024最新超详细无痛安装PyTorch,包括下载地址、环境配置与各种避坑指南

PyTorch更新很快,2024最新下载安装经验贴,包括anaconda+CUDA+cuDNN+PyTorch四部分的下载和安装。文章详细介绍了下载地址、环境配置、虚拟环境建立的问题,还温馨提示了各种闭坑指南。有需要的友友快过来康康!

qq_43596278的博客 11万+

UVA - 1069 Always an integer (模拟)

Description Combinatorics is a branch of mathematics chiefly concerned with counting discrete objects. For instance, how many ways can you pick two people out of a crowd of n people? Into how many

那些年 1508

【2026年米哈游春招 - 3月14日 -第一题- 行列相等】(题目+思路+Java&C++&Python解析+在线测试)

摘要 本文解决了一个矩阵行列和匹配问题:统计矩阵中行和等于列和的所有行列对数量。算法首先计算每行和每列的元素和,然后利用哈希表统计各行列和的出现次数。通过遍历列和并查询对应的行和出现次数,高效计算出匹配对数。该方法时间复杂度为O(nm),空间复杂度为O(n+m),适用于大规模矩阵数据。代码实现了Python、Java和C++版本,通过哈希表优化避免了暴力枚举,显著提高了计算效率。

m0_73659489的博客 86

基于YOLOv8的铁路轨道异物检测系统设计与实现

目标检测是计算机视觉领域的核心任务之一,旨在识别图像中特定目标的位置和类别。传统方法主要依赖手工特征(如HOG、SIFT等)和分类器(如SVM),而深度学习方法通过卷积神经网络自动学习特征表示,显著提升了检测性能。YOLO(You Only Look Once)系列算法作为单阶段目标检测的代表,以其高效的检测速度著称。YOLOv8是Ultralytics公司于2023年发布的最新版本,在精度和速度上都有进一步提升。

YOLO 584

LA 4119 Always an integer (数学)

LA 4119 Always an integer题目大意:给定一个形如(P)/D的多项式,其中P是若干个形如Cn^E的项之和,判断他是否在所有正整数处取到整数值. 其中系数C和次数E满足如下条件: 1.E是满足0<=E<=100的整数.若E=0,则Cn^E写成C;若E=1,则Cn^E写成Cn,但当C=±1时除外(C=1时,写成n;C=-1时,写成-n). 2.C为整数.若C=±1,且E不是0

Continue 583

100:vue+cesium: 加载glb文件,随机位置加载2000架飞机

本示例在 Vue + cesium 项目中随机位置加载2000架飞机,飞机是glb文件,每个大小为571kb。这个示例属于性能优化的范畴内,通常如果用entity加载glb只能显示几个飞机,这里采用primitive的方法来加载。直接复制下面的 vue+cesium源代码,操作2分钟即可运行实现效果.

大剑师兰特的GIS世界 720

uva1069 Always an integer【解法一】

随机化

YHaX的博客 512

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

UVA1069 Always an integer

数学分析

ACoder 228

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

uva1069 Always an integer【解法二】

结论题

YHaX的博客 408

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

uva 1069 - Always an integer(数学+暴力)

题目链接:uva 1069 - Always an integer 题目大意:给出一个多次多项式,问说是否对于任意正整数n来说结构均为整数。 解题思路:首先处理出字符串,然后枚举n从1到k+1判断即可,k为多项式中出现过的最大幂数指。 P为多项式,d为除数,k为多项式中最大的次数 当k=0时,P中不存在n变量,所以直接计算判断即可当k=1时,P是一次多项式,那么P(n+

不慌不忙、不急不躁 1518

Always an integer UVA - 1069(多项式的差分数列+模拟)

#include&lt;bits/stdc++.h&gt; using namespace std; #define rep(i,a,b) for(int i=a;i&lt;b;i++) #define per(i,a,b) for(int i=b-1;i&gt;=a;i--) #define bug(x) printf("x:%d****\n",x) typedef long long ...

海绵的博客 242

ACM: uva 1069 -&n…

Always an integer Combinatorics is a branch of mathematics chiefly concerned with counting discrete objects. For instance, how many ways can you pick two people out of a crowd of n  n  1×1  n×n  uva 1

smile_benson 716

uvaoj 1069 Always an integer

题解: 1.对于最高次数是n次的时候,为x分别带入0,1,2…到n + 1,如果都为整数,则多项式为整数 总结: 1.这个题目没有自己想出来,总结一下,以后在做题的时候还是要明确目标,猜猜可能能通过哪些方面得到答案 2.这道题依旧碰到了bug,导致浪费了很多时间,以后做到,每次犯错误,都停下来思考一下,对以后要做的事情做一个简单的规划 3.昨天写xv6的编程作业,感觉都几乎已经能够写完了,

c337134154的专栏 616

UVA 1069

#include #include #include #include #include #define LL long long using namespace std; LL d,c[105]; char s[10005]; LL pow_mod(LL x,LL y){ LL ret=1; while(y){ if(y&1) ret=ret*x

AB 799

6481: Integers on a Tree

6481: Integers on a Tree时间限制: 1 Sec  内存限制: 128 MB提交: 69  解决: 12[提交][状态][讨论版][命题人:admin]题目描述We have a tree with N vertices. The vertices are numbered 1,2,…,N. The i-th (1≤i≤N−1) edge connects the two v...

Acerkoo的博客 554

leetcode 部分题解(python)

leetcode 部分题解(python)Sum of Two Integers题目链接:https://leetcode.com/problems/sum-of-two-integers/ 题目翻译: 计算两个整数a和b的和,但是不能使用运算符加号和减号。 比如:给定a=1,b=2,返回3。思路:既然不能使用加法和减法,那么就用位操作。下面以计算5+4的例子说明如何用位操作实现加法:

wanghai00的博客 6152

1069 - Always an integer

Combinatorics is a branch of mathematics chiefly concerned with counting discrete objects. For instance, how many ways can you pick two people out of a crowd of n people? Into how many regions can y

784

uva 10691 - Subway(区间选点)

题目链接:uva 10691 - Subway 题目大意:给定n个点,要求建造尽量少得铁路(从原点发射出的射线),使得所有点到铁路的最短距离小于d。 解题思路:题目可以转化成区间选点问题,即以极角来表示铁轨,然后计算出每个区间可行的极角范围,进行区间选点。 注意:(1)如果点到原点的距离dis (2)区间有可能在二三象限时重叠,我的处理方法是每次枚举起始点,进行n次选点问题

不慌不忙、不急不躁 1390
上一篇: uva10237 - Bishops 互不攻击的象
下一篇: uva10692 - Huge Mods poj2164: Remainder Calculator 指数循环节
小小小小葱
博客等级 码龄14年 22粉丝 266原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址