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. mysql 性能问题的解决

    场景:模拟一天的数据,每个10秒,遍历1000个设备,每个设备模拟一个实时数据,总的数据量为:24*60*60/10*1000 = 864万条记录.-------------------------- ...

  2. Nginx反向代理负载均衡

    环境准备: 总共四台机器,两台装有Nginx的机器做负载均衡,两台机器装有Apache作为WEB服务器. 机器信息 hostname IP 说明 lb01 192.168.1.19 nginx主负载均 ...

  3. 2 CSS

    2 CSS CSS基础 html 在一个网页中负责的事情是一个页面的结构css(层叠样式表) 在一个网页中主要负责了页面的数据样式. 编写css代码的方式:     第一种: 在style标签中编写c ...

  4. hdu 4288 Coder

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

  5. flume Permission denied: user=flume, access=WRITE, inode

    My flume app is attempting to write to HDFS on a path thats not been created/granted for it. The pat ...

  6. Java.util.concurrent包学习(一) BlockingQueue接口

    JDK1.7 BlockingQueue<E>接口 (extends Queue<E>) 所有父接口:Collection<E>,Iterable<E> ...

  7. vim Project

    VIM是Linux和Unix下常用的文本编辑工具,在编写代码和阅读代码中经常使用. 但VIM进行代码项目管理时,没有IDE集成开发工具方便,现在提供一个VIM插件Project,可以对代码项目进行简单 ...

  8. CRLF CSRF XSS

    http://baike.baidu.com/link?url=BXWN2I6J23thrrm6JoEnAYvmNqp83llyaydaj5RYkq--tuJKSFuMuDMIoTPnKjthRUm3 ...

  9. drupal里面的ajax最粗浅的理解-流程

    1,  form里面的ajax所在地表单元素有一个事件,激发system/ajax,相应的有ajax_form_callback(), 会把被改变的元素值传到form_state[values]中, ...

  10. 扩展Date的DateAdd方法--计算日期

    Date.prototype.DateAdd = function(strInterval, Number) { var dtTmp = this; switch (strInterval) { ca ...