您的位置:首页 > 其它

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

2014-11-17 13:17 330 查看
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraudAlways an integerTime Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %lluCombinatorics 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?
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
string s;
vector<pair<long long,long long> >vec;
long long fast_mod(long long m,long long n,long long fenm)
{
long long ret=1;
long long temp=m;
while(n)
{
if(n&1)
{
ret=ret*temp;
ret%=fenm;
}
temp=temp*temp;
temp%=fenm;
n>>=1;
}
return ret;
}
int main()
{
ios::sync_with_stdio(false);
int cas=1;
//freopen("in.in","r",stdin);
while(cin>>s)
{
if(s==".")break;
int len=s.length();
vec.clear();
int pos=0;
while(pos<len&&s[pos]!='/')pos++;
long long fenm=0;
int index=pos+1;
while(index<len)    fenm=s[index++]-'0'+fenm*10;
if(fenm==0)fenm=1;
long long a,b;
long long maxx=0;
bool chac=0;
for(int i=0;i<pos;)
{
chac=0;
a=0,b=0;
if(s[i]=='(')i++;
if(s[i]==')'||s[i]=='/')break;
if(s[i]=='+'||s[i]=='-')
{
if(s[i]=='-')chac=1;
i++;
}
while(i<pos&&s[i]!='/'&&s[i]!='n'&&s[i]!=')')
{
a*=10;
a+=s[i++]-'0';
}
if(a==0)a=1;
if(chac)a*=-1;
if(s[i]=='/'||s[i]==')')
{
vec.push_back(make_pair(a,0));
break;
}
i++;
if(s[i]=='^')i++;
while(i<pos&&s[i]>='0'&&s[i]<='9')
{
b*=10;
b+=s[i++]-'0';
}
if(b==0)b=1;
vec.push_back(make_pair(a,b));
maxx=max(b,maxx);
}
bool flag=0;
long long temp;
for(int i=1;i<=maxx+1;i++)
{
temp=0;
for(int j=0;j<vec.size();j++)
{
temp+=(vec[j].first*fast_mod(i,vec[j].second,fenm))%fenm;
temp%=fenm;
}
if(temp){flag=1;break;}
}
cout<<"Case "<<cas++<<": ";
if(flag)cout<<"Not always an integer"<<endl;
else cout<<"Always an integer"<<endl;
s.clear();
}
return 0;
}
View Code

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