https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2120

假设式子的最高幂次为k 则只需要测试1到k+1就可以了

原理见 刘汝佳的《算法竞赛入门经典入门指南》 P123

字符串需要自己处理,比较繁琐 一定要细心

代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue> #define ll long long
#define lint long long
using namespace std; const int N=100005;
ll a[N];
ll powerMod(ll x,ll y,ll M)
{//cout<<x<<" "<<y<<" "<<M<<endl;
ll tmp=1;
while(y)
{
if(y&1)
tmp=tmp*x%M;
x=x*x%M;
y=y>>1;
}
//cout<<(tmp%M)<<endl;
return tmp%M;
}
void func(string s)
{//cout<<s<<endl;
if(s.size()==0) return; int m=s.find('n');
if(m==-1)
m=s.size();
int type=(s[0]=='-')?-1:1;
ll tmp=0;
//int l=;cout<<l<<" "<<m<<endl;
for(int i=(s[0]=='+'||s[0]=='-')?1:0;i<m;++i)
{//cout<<i<<" "<<tmp<<endl;
tmp=tmp*10+s[i]-'0';
}
if(tmp==0) tmp=1;
tmp=tmp*type;
m=s.find('n');//cout<<m<<endl;
if(m==-1) a[0]=tmp;
else
{//cout<<"in1"<<endl;return ;
int k=s.find('^');
if(k==-1)
a[1]=tmp;
else
{//cout<<"in2"<<endl;
int x=0;
for(int i=k+1;i<s.size();++i)
{
x=x*10+s[i]-'0';
}
//cout<<x<<" "<<tmp<<endl;
a[x]=tmp;
}
}
//cout<<"return"<<endl;
}
bool solve(string s)
{
memset(a,0,sizeof(a));
int k=s.find('/');
ll M=0;
for(int i=k+1;i<s.length();++i)
M=M*10+s[i]-'0';
string stmp;
for(int i=(s[0]=='(')?1:0;i<k&&s[i]!=')';++i)
{
if(s[i]=='+'||s[i]=='-')
{
func(stmp);
stmp.clear();
}
stmp.push_back(s[i]);
}
func(stmp);
for(ll i=1;i<=101;++i)
{//cout<<i<<endl;
ll tmp=0;
for(ll j=0;j<=100;++j)
{
//cout<<j<<" "<<a[j]<<endl;
tmp=(tmp+a[j]*powerMod(i,j,M))%M;
}
if(tmp!=0)
return false;
}
return true;
}
int main()
{
//freopen("data.in","r",stdin);
int ca=1;
string s;
while(cin>>s)
{
if(s==".")break;
if(solve(s))
printf("Case %d: Always an integer\n",ca++);
else
printf("Case %d: Not always an integer\n",ca++);
}
return 0;
}

LA 4119 - Always an integer的更多相关文章

  1. LA 4119 Always an integer (数论+模拟)

    ACM-ICPC Live Archive 一道模拟题,题意是问一个给出的多项式代入正整数得到的值是否总是整数. 这题是一道数论题,其实对于这个式子,我们只要计算1~最高次项是否都满足即可. 做的时候 ...

  2. LA 4119 (差分数列 多项式) Always an integer

    题意: 给出一个形如(P)/D的多项式,其中P是n的整系数多项式,D为整数. 问是否对于所有的正整数n,该多项式的值都是整数. 分析: 可以用数学归纳法证明,若P(n)是k次多项式,则P(n+1) - ...

  3. UVALive 4119 Always an integer (差分数列,模拟)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Always an integer Time Limit:3000MS     M ...

  4. .Uva&LA部分题目代码

    1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...

  5. 835. Image Overlap

    Two images A and B are given, represented as binary, square matrices of the same size.  (A binary ma ...

  6. CSU训练分类

    √√第一部分 基础算法(#10023 除外) 第 1 章 贪心算法 √√#10000 「一本通 1.1 例 1」活动安排 √√#10001 「一本通 1.1 例 2」种树 √√#10002 「一本通 ...

  7. REDIS基础笔记

    Redis基础笔记 资源链接 简介 简介 安装 五种数据类型及相应命令 1. 字符串类型 2. 散列类型 3. 列表类型 4. 集合类型 5. 有序集合 其他 事务 SORT 生存时间 任务队列 发布 ...

  8. java实现顺序表、链表、栈 (x)->{持续更新}

    1.java实现节点 /** * 节点 * @luminous-xin * @param <T> */ public class Node<T> { T data; Node& ...

  9. LC 835. Image Overlap

    Two images A and B are given, represented as binary, square matrices of the same size.  (A binary ma ...

随机推荐

  1. UVA 11468【AC自动机+DP】

    dp[i][j]表示走了i步走到j结点的概率.初始值dp[0][0] = 1.当走到的结点不是单词尾结点时,才能走过去. !end[i]&&last[i] == root时,该结点才可 ...

  2. oracle linux 下卸载

    1. 关闭数据库 shutdown immeidate 2. 停止 Listener lsnrctl stop 3. 停止http服务(可选) service httpd stop 4. 用su或者重 ...

  3. js打印出对象的方法

    var description = ""; for (var i in order) { var property = order[i]; description += i + & ...

  4. (十)Linux内核中的常用宏container_of

    Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. Containe ...

  5. hdu 4288 Coder

    线段树好题,和 15 年的广东省省赛 C 题有相似之处,一开始我的思路有偏差,看了别人的博客后感觉处处技巧都是精华,主要是区间合并的技巧一时很难想到,先附上代码: #include<cstdio ...

  6. MFC编程入门之前言

    本系列主要偏重于理论方面的知识,目的是打好底子,练好内功,在使用VC++编程时不至于丈二和尚摸不着头脑.本系列也会涉及到VC++的原理性的东西,同样更重视实用性,学完本系列以后,基本的界面程序都能很容 ...

  7. Vbs脚本经典教材(转)

    Vbs脚本经典教材(最全的资料还是MSDN) —为什么要使用Vbs? 在Windows中,学习计算机操作也许很简单,但是很多计算机工作是重复性劳动,例如你每周也许需要对一些计算机文件进行复制.粘贴.改 ...

  8. python类、对象、方法、属性之类与对象笔记

    python中一切皆为对象,所谓对象:我自己就是一个对象,我玩的电脑就是对象,坐着的椅子就是对象,家里养的小狗也是一个对象...... 我们通过描述属性(特征)和行为来描述一个对象的.比如家里的小狗, ...

  9. ITERATOR(迭代器)设计模式

    1 意图:提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部表示. 2 别名(Cursor) 3 动机:队列表的访问和遍历从列表对象中分离出来放入一个迭代器对象中.   多态迭代   ...

  10. hiho1092_have lunch together

    题目 两个人从同一个点出发,在一个餐厅中寻找两个相邻的座位,需要是的从出发点到达座位的距离总和最短.题目链接: Have Lunch Together     最短路程,一开始以为要用dijkstra ...