题目大意:
给定一个形如(P)/D的多项式,其中P是若干个形如Cn^E的项之和,判断他是否在所有正整数处取到整数值.
其中系数C和次数E满足如下条件:
1.E是满足0<=E<=100的整数.若E=0,则Cn^E写成C;若E=1,则Cn^E写成Cn,但当C=±1时除外(C=1时,写成n;C=-1时,写成-n).
2.C为整数.若C=±1,且E不是0或者1,则Cn^E写成n^E或者-n^E.
3.只有不在第一项的非负C前面有加号.
4.E数值严格递减.
5.C和D都在32位带符号整数范围内.
题目分析:
首先考虑如何判断该多项式是否总是整数.
可以先举例观察,设最高次数为k:
当k=0时,P为常量,求出P(1)即可.
当k=1时,P是n的一次多项式,设P=an+b,则有P(2)-P(1)=a.此时将{P(n)}看成一个数列,首项为P(1),公差为a=P(2)-P(1).所以只要求出P(1),P(2)就可以判断了.
当k=2时,P是n的二次多项式,设P=an^2+bn+c,则有P(n+1)-P(n)=2an+a+b,根据在k=1中求出的结论,也只要判断n=1和n=2能被D整除即可.也就是要求P(1),P(2),P(3)三项即可.
有数学归纳法就可以得到,对于一个最高次为k的多项式而言,只需要判断当n=1~k+1时,能否有P(n)整除D,就可以判断了.
当然此题还有一个问题——输入问题,
先考虑P,因为P中每个单项式C和E都是有可能没有的,逐行读入,可以选择以n为分界,来分解每一个多项式的系数和指数。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N=500001;
char str[N];
int n,d;
LL a[N],b[N];//a: power ;b : xishu
LL Calc(LL x)
{
LL Ans=0;
for (int i=1;i<=n;i++){
LL tmp=1;
for (int j=1;j<=a[i];j++)tmp=tmp*x%d;
Ans=(Ans+tmp*b[i]%d)%d;
}
return Ans;
}
int main()
{
//freopen("1.txt","r",stdin);
int Case=0;
while (scanf("%s",str)!=EOF){
if (str[0]=='.')break;
Case++;printf("Case %d: ",Case);
n=0;
int L=strlen(str),i,f;
for (i=1;i<L;i++){
if (str[i]==')')break;
n++;
if (str[i]=='-' || str[i]=='+'){
if (str[i+1]=='n')b[n]=(str[i]=='+')?1:-1;else {
int j=i+1;b[n]=0;
while ('0'<=str[j] && str[j]<='9')b[n]=b[n]*10+str[j]-'0',j++;
b[n]=(str[i]=='+')?b[n]:-b[n];
i=j-1;
}
}else
if (str[i]=='n'){//第一个
b[n]=1;i--;
}else {
int j=i;b[n]=0;
while ('0'<=str[j] && str[j]<='9')b[n]=b[n]*10+str[j]-'0',j++;
i=j-1;
}
if (str[i+1]=='n'){
if (str[i+2]=='^'){
int j=i+3;a[n]=0;
while ('0'<=str[j] && str[j]<='9')a[n]=a[n]*10+str[j]-'0',j++;
i=j-1;
}else {a[n]=1;i++;}
}else a[n]=0;
}
//for (int u=1;u<=n;u++)cout<<a[u]<<" "<<b[u]<<endl;
i+=2;d=0;
for (;i<L;i++)d=d*10+str[i]-'0';
int Flag=1;
for (int i=1;i<=a[1]+1;i++){
if (Calc(i))Flag=0;
}
if (Flag)puts("Always an integer");
else puts("Not always an integer");
}
return 0;
}
代码写法2:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100+5;
const int maxl=2000+10;
const char* output[]={"Not always an integer","Always an integer"};
void getnum(char* s,int& pos,int& c,int& e)//c*n^e 一次性读取c和e两数
{
int flag=1;c=e=0;
while((s[pos]<'0'||s[pos]>'9')&&s[pos]!='n') if(s[pos++]=='-') flag=-1;
while(s[pos]>='0'&&s[pos]<='9') c=c*10+s[pos]-'0',++pos;
if(!c) c=1; c*=flag;//若没有读到c |c|=1
int have=0;//若没有读到e 有'n',e=1 没有'n',e=0
if(s[pos]=='n') {
have=1,++pos;
if(s[pos]=='^') ++pos;
while(s[pos]>='0'&&s[pos]<='9') e=e*10+s[pos]-'0',++pos;
}
if(!e) e=have; --pos;
}
int C[maxn],maxe,D,kase=0;
bool check(int n)
{
int ans=0,p=1;//可以选择不用快速幂,递推求n的各次幂
for(int e=0;e<=maxe;e++,p=(1ll*p*n)%D)
if(C[e]) ans=(0ll+ans+1ll*C[e]*p)%D;
return ans==0;
}
void solve()
{
int always=1;//从1到maxe+1依次判断
for(int i=1;i<=maxe+1;i++) if(!check(i)) {always=0;break;}
printf("Case %d: %s\n",++kase,output[always]);
}
char buf[maxl];
bool input()
{
scanf("%s",buf);
if(buf[0]=='.'&&buf[1]=='\0') return false;//结束标志
memset(C,0,sizeof(C));
int i,c,e;maxe=D=0;
for(i=0;buf[i]!=')';i++) {//选择两个两个读入
getnum(buf,i,c,e);
C[e]+=c;maxe=max(maxe,e);
}
i+=2;//最后读入D
while(buf[i]>='0'&&buf[i]<='9') D=D*10+buf[i]-'0',++i;
return true;
}
int main()
{
while(input()) solve();
return 0;
}