zoukankan      html  css  js  c++  java
  • uvalive 4119 Always an Interger

    差分数列+字符串处理

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

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

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

      1 #include <iostream>
      2 #include <cstdio>
      3 #include<cstdlib>
      4 #include<cctype>
      5 #include<string>
      6 #include<vector>
      7 #include<cassert>
      8 using namespace std;
      9 struct Polynomial
     10 {
     11     vector<int >a,p;
     12     void parse_polynomial(string str)
     13     {
     14         int len=str.size(),i=0;
     15         while(i<len)
     16         {
     17             int sign=1;
     18             if(str[i]=='+')
     19                 i++;
     20             if(str[i]=='-')
     21             {
     22                 i++;
     23                 sign=-1;
     24             }
     25             int v=0;
     26             while(i<len&&isdigit(str[i]))
     27                 v=v*10+str[i++]-'0';
     28             if(i==len)
     29             {
     30                 a.push_back(v);
     31                 p.push_back(0);
     32             }
     33             else
     34             {
     35                 assert(str[i] == 'n');
     36                 if(v==0)
     37                     v=1;
     38                 v*=sign;
     39                 if(str[++i]=='^')
     40                 {
     41                     a.push_back(v);
     42                     v=0;
     43                     i++;
     44                     while(i<len&&isdigit(str[i]))
     45                         v=v*10+str[i++]-'0';
     46                     p.push_back(v);
     47                 }
     48                 else
     49                 {
     50                     a.push_back(v);
     51                     p.push_back(1);
     52                 }
     53             }
     54         }
     55     }
     56         int mod(int x,int MOD)
     57         {
     58             int n=a.size();
     59             int ans=0;
     60             for(int i=0; i<n; i++)
     61             {
     62                 int m=a[i];
     63                 for(int j=0; j<p[i]; j++)
     64                 {
     65                     m=(long long)m*x%MOD;
     66                 }
     67                 ans=((long long)ans + m) % MOD;
     68             }
     69             return ans;
     70         }
     71 };
     72 
     73 bool check(string str)
     74 {
     75     int p=str.find('/');
     76     Polynomial poly;
     77     poly.parse_polynomial(str.substr(1, p-2));
     78     int D = atoi(str.substr(p+1).c_str());
     79     for(int i=1; i<=poly.p[0]+1;i++)
     80     {
     81         if(poly.mod(i,D))
     82             return false;
     83     }
     84     return true;
     85 }
     86 
     87 int main()
     88 {
     89     int cas=1;
     90     string str;
     91     while(cin>>str)
     92     {
     93         if(str[0]=='.')
     94             break;
     95         printf("Case %d: ", cas++);
     96             if(check(str))
     97                 printf("Always an integer
    ");
     98             else
     99                 printf("Not always an integer
    ");
    100     }
    101     return 0;
    102 }
    View Code
  • 相关阅读:
    python-函数进阶
    SHELL wordpress.sh
    XFS: possible memory allocation deadlock in kmem_alloc (mode:0x2d0)
    Puppet install with nginx unicorn
    CentOS 6内核编译问题整理
    Openstack 本地yum源配置
    Openstack 本地yum源配置
    hpsa 0000:0a:00.0: out of memory
    openstack VNC安全问题
    CentOS下crash分析内核kdump文件方法
  • 原文地址:https://www.cnblogs.com/ITUPC/p/4912881.html
Copyright © 2011-2022 走看看