LA 4119

来源:互联网 发布:python 结巴分词下载 编辑:程序博客网 时间:2024/04/27 23:22
#include <iostream>#include <cstdio>#include<cstdlib>#include<cctype>#include<string>#include<vector>#include<cassert>using namespace std;struct Polynomial{    vector<int >a,p;    void parse_polynomial(string str)    {        int len=str.size(),i=0;        while(i<len)        {            int sign=1;            if(str[i]=='+')                i++;            if(str[i]=='-')            {                i++;                sign=-1;            }            int v=0;            while(i<len&&isdigit(str[i]))                v=v*10+str[i++]-'0';            if(i==len)            {                a.push_back(v);                p.push_back(0);            }            else            {                assert(str[i] == 'n');                if(v==0)                    v=1;                v*=sign;                if(str[++i]=='^')                {                    a.push_back(v);                    v=0;                    i++;                    while(i<len&&isdigit(str[i]))                        v=v*10+str[i++]-'0';                    p.push_back(v);                }                else                {                    a.push_back(v);                    p.push_back(1);                }            }        }    }        int mod(int x,int MOD)        {            int n=a.size();            int ans=0;            for(int i=0; i<n; i++)            {                int m=a[i];                for(int j=0; j<p[i]; j++)                {                    m=(long long)m*x%MOD;                }                ans=((long long)ans + m) % MOD;            }            return ans;        }};bool check(string str){    int p=str.find('/');    Polynomial poly;    poly.parse_polynomial(str.substr(1, p-2));    int D = atoi(str.substr(p+1).c_str());    for(int i=1; i<=poly.p[0]+1;i++)    {        if(poly.mod(i,D))            return false;    }    return true;}int main(){    int cas=1;    string str;    while(cin>>str)    {        if(str[0]=='.')            break;        printf("Case %d: ", cas++);            if(check(str))                printf("Always an integer\n");            else                printf("Not always an integer\n");    }    return 0;}


题意:是让你判断一个整系数多项式的值是否一直都能被一个所给的正整数所整除。


通过对差分数列的不断求导,我们可以发现,对于任意多项式P,我们只需要判断n从1到k+1是否满足就行了,其中,k为多项式P中的最高次数。


可以先了解一下差分数列。


来自于 : http://www.cnblogs.com/ITUPC/p/4912881.html

0 0
原创粉丝点击