LA 4119
来源:互联网 发布:python 结巴分词下载 编辑:程序博客网 时间:2024/04/27 23:22
#include <iostream>#include <cstdio>#include<cstdlib>#include<cctype>#include<string>#include<vector>#include<cassert>using namespace std;struct Polynomial{ vector<int >a,p; void parse_polynomial(string str) { int len=str.size(),i=0; while(i<len) { int sign=1; if(str[i]=='+') i++; if(str[i]=='-') { i++; sign=-1; } int v=0; while(i<len&&isdigit(str[i])) v=v*10+str[i++]-'0'; if(i==len) { a.push_back(v); p.push_back(0); } else { assert(str[i] == 'n'); if(v==0) v=1; v*=sign; if(str[++i]=='^') { a.push_back(v); v=0; i++; while(i<len&&isdigit(str[i])) v=v*10+str[i++]-'0'; p.push_back(v); } else { a.push_back(v); p.push_back(1); } } } } int mod(int x,int MOD) { int n=a.size(); int ans=0; for(int i=0; i<n; i++) { int m=a[i]; for(int j=0; j<p[i]; j++) { m=(long long)m*x%MOD; } ans=((long long)ans + m) % MOD; } return ans; }};bool check(string str){ int p=str.find('/'); Polynomial poly; poly.parse_polynomial(str.substr(1, p-2)); int D = atoi(str.substr(p+1).c_str()); for(int i=1; i<=poly.p[0]+1;i++) { if(poly.mod(i,D)) return false; } return true;}int main(){ int cas=1; string str; while(cin>>str) { if(str[0]=='.') break; printf("Case %d: ", cas++); if(check(str)) printf("Always an integer\n"); else printf("Not always an integer\n"); } return 0;}
题意:是让你判断一个整系数多项式的值是否一直都能被一个所给的正整数所整除。
通过对差分数列的不断求导,我们可以发现,对于任意多项式P,我们只需要判断n从1到k+1是否满足就行了,其中,k为多项式P中的最高次数。
可以先了解一下差分数列。
来自于 : http://www.cnblogs.com/ITUPC/p/4912881.html
0 0
- LA 4119
- LA 4119 总是整数
- la la la, la la la
- LA 4119 Always an integer (数学)
- LA 3942
- LA 4670
- LA 3135
- LA 3644
- LA 3027
- LA 5059
- LA 3644
- La 3942
- LA 3126
- LA 4329
- LA 3384
- LA 4329
- LA 2995
- LA 3135
- JVM性能参数调优实践,不会执行Full GC,网站无停滞
- 算法学习--高级数据结构
- 【小镇的技术天梯】Linux 虚拟内存和物理内存的理解
- java分布式对象RMI应用测试用例
- ubuntu14.04 nvidia760 960驱动 集成、独立显卡切换 安装x server
- LA 4119
- DC4C代码阅读(9)——register node基础结构
- C++中map的使用详解说明
- VMware Workstation 12序列号
- 在Android studio中如何使用lambda?
- Linux下的环境变量设置
- PHP读取XML
- 面向对象02--java基础
- PHP创建XML