您的位置:首页 > 其它

worldfinals 2008 Always an integer

2013-04-22 16:10 525 查看
利用差分序列 p(n) 为K 次多项式 p(0)p(1) p(2) .........p(k) 均整除D 其一阶 差分序列 dp(0),dp(1)....dp(n) 均能整除D,类推可知 d^kp(0),d^kp(1).....可整除D,且差分序列最后 变为常数

#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<iostream>
#include<string>
#include<sstream>
#include<cctype>
#include<set>
#include<stack>
#include<functional>
#include<memory>
#include<deque>
#include<list>
#include<cmath>
#include<fstream>
#include<cstdlib>
#include<climits>
#include<iomanip>
#include<cstring>
#include<memory>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef vector<int>VI;
typedef vector<VI>VVI;
typedef vector<VVI>VVVI;
const int maxn = 711;
const int mod = 1000000007;
template<class T> T gcd(T a,T b){ return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){ return a/gcd(a,b)*b;}
char buf[maxn<<2];
ll POW(ll a,ll b,ll c)
{
ll res=1;
while(b)
{
if(b&1) res=res*a%c;
a=a*a%c;
b>>=1;
}
return res;
}
ll E[maxn],p[maxn],D;
int cnt=0;
bool cal(int k,int n)
{
ll res=0,t;
int i;
for(i=0;i<cnt;++i)
{
t=E[i]%D;
t=t*POW(k,p[i],D)%D;
res=(res+t)%D;
}
return res==0;
}
bool ok(char ch)
{
return ch=='+'||ch=='-';
}
int main()
{
ios::sync_with_stdio(false);
int cas=0,i,j,beg;
while(true)
{
cas++;
cin>>buf;
if(buf[0]=='.') break;
char ch;
cnt=0;
memset(p,0,sizeof(p));
int sign,n,len=strlen(buf);
if(buf[1]!='-') beg=0,buf[0]='+';
else beg=1;
for(i=beg;i<len;)
{
if(buf[i]=='/')
{
D=0;
for(j=i+1;j<len;++j)
{
D=D*10+(buf[j]-'0');
}
break;
}
if(ok(buf[i]))
{
sign=1;
if(buf[i]=='-') sign=-1;
E[cnt]=0;
for(j=i+1;buf[j]!='n'&&!ok(buf[j])&&buf[j]!=')';++j)
{
E[cnt]=E[cnt]*10+(buf[j]-'0');
}
E[cnt]*=sign;
if(!E[cnt])
E[cnt]=sign;
i=j;
p[cnt]=0;
if(buf[i]=='n')
{
if(buf[i+1]=='^')
{
p[cnt]=0;
for(j=i+2;!ok(buf[j])&&buf[j]!=')';++j)
{
p[cnt]=p[cnt]*10+(buf[j]-'0');
}
}
else
j=i+1,p[cnt]=1;
}
i=j;
if(buf[i]==')')
++i;
cnt++;
}
}
bool flag=true;
for(n=0;n<=p[0];++n)
{
flag&=cal(n,cnt);
if(!flag)
break;
}
cout<<"Case "<<cas<<": ";
if(flag)
cout<<"Always an integer"<<endl;
else
cout<<"Not always an integer"<<endl;

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: