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

转载请注明出处: http://www.cnblogs.com/fraud/           ——by fraud

Always an integer

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Combinatorics 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?

epsfbox{p4119.eps}

Many questions like these have answers that can be reduced to simple polynomials in n . The answer to the first question above is n(n - 1)/2 , or (n$scriptstyle wedge$2 - n)/2 . The answer to the second is (n$scriptstyle wedge$4 - 6n$scriptstyle wedge$3 + 23n$scriptstyle wedge$2 - 18n + 24)/24 . The answer to the third is n(n + 1)(2n + 1)/6 , or (2n$scriptstyle wedge$3 + 3n$scriptstyle wedge$2 + n)/6 . We write these polynomials in a standard form, as a polynomial with integer coefficients divided by a positive integer denominator.

These polynomials are answers to questions that can have integer answers only. But since they have fractional coefficients, they look as if they could produce non-integer results! Of course, evaluating these particular polynomials on a positive integer always results in an integer. For other polynomials of similar form, this is not necessarily true. It can be hard to tell the two cases apart. So that, naturally, is your task.

Input

The input consists of multiple test cases, each on a separate line. Each test case is an expression in the form (P)/D , where P is a polynomial with integer coefficients and D is a positive integer denominator. P is a sum of terms of the form Cn$scriptstyle wedge$E , where the coefficient C and the exponent E satisfy the following conditions:

  1. E is an integer satisfying 0$ le$E$ le$100 . If E is 0, then Cn$scriptstyle wedge$E is expressed as C . If E is 1, then Cn$scriptstyle wedge$E is expressed as Cn , unless C is 1 or -1. In those instances, Cn$scriptstyle wedge$E is expressed as n or - n .
  2. C is an integer. If C is 1 or -1 and E is not 0 or 1, then the Cn$scriptstyle wedge$E will appear as n$scriptstyle wedge$E or - n$scriptstyle wedge$E .
  3. Only non-negative C values that are not part of the first term in the polynomial are preceded by +.
  4. Exponents in consecutive terms are strictly decreasing.
  5. C and D fit in a 32-bit signed integer.

See the sample input for details.

Input is terminated by a line containing a single period.

Output

For each test case, print the case number (starting with 1). Then print `Always an integer' if the test case polynomial evaluates to an integer for every positive integer n . Print ` Not always an integer' otherwise. Print the output for separate test cases on separate lines. Your output should follow the same format as the sample output.

Sample Input

(n^2-n)/2 
(2n^3+3n^2+n)/6 
(-n^14-11n+1)/3 
.

Sample Output

Case 1: Always an integer 
Case 2: Always an integer 
Case 3: Not always an integer

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

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

接下来就是纯模拟了。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <vector>
 4 #include <algorithm>
 5 #include <cstdio>
 6 using namespace std;
 7 string s;
 8 vector<pair<long long,long long> >vec;
 9 long long fast_mod(long long m,long long n,long long fenm)
10 {
11     long long ret=1;
12     long long temp=m;
13     while(n)
14     {
15         if(n&1)
16         {
17             ret=ret*temp;
18             ret%=fenm;
19         }
20         temp=temp*temp;
21         temp%=fenm;
22         n>>=1;
23     }
24     return ret;
25 }
26 int main()
27 {
28     ios::sync_with_stdio(false);
29     int cas=1;
30     //freopen("in.in","r",stdin);
31     while(cin>>s)
32     {
33         if(s==".")break;
34         int len=s.length();
35         vec.clear();
36         int pos=0;
37         while(pos<len&&s[pos]!='/')pos++;
38         long long fenm=0;
39         int index=pos+1;
40         while(index<len)    fenm=s[index++]-'0'+fenm*10;
41         if(fenm==0)fenm=1;
42         long long a,b;
43         long long maxx=0;
44         bool chac=0;
45         for(int i=0;i<pos;)
46         {
47             chac=0;
48             a=0,b=0;
49             if(s[i]=='(')i++;
50             if(s[i]==')'||s[i]=='/')break;
51             if(s[i]=='+'||s[i]=='-')
52             {
53                 if(s[i]=='-')chac=1;
54                 i++;
55             }
56             while(i<pos&&s[i]!='/'&&s[i]!='n'&&s[i]!=')')
57             {
58                 a*=10;
59                 a+=s[i++]-'0';
60             }
61             if(a==0)a=1;
62             if(chac)a*=-1;
63             if(s[i]=='/'||s[i]==')')
64             {
65                 vec.push_back(make_pair(a,0));
66                 break;
67             }
68             i++;
69             if(s[i]=='^')i++;
70             while(i<pos&&s[i]>='0'&&s[i]<='9')
71             {
72                 b*=10;
73                 b+=s[i++]-'0';
74             }
75             if(b==0)b=1;
76             vec.push_back(make_pair(a,b));
77             maxx=max(b,maxx);
78         }
79         bool flag=0;
80         long long temp;
81         for(int i=1;i<=maxx+1;i++)
82         {
83             temp=0;
84             for(int j=0;j<vec.size();j++)
85             {
86                 temp+=(vec[j].first*fast_mod(i,vec[j].second,fenm))%fenm;
87                 temp%=fenm;
88             }
89             if(temp){flag=1;break;}
90         }
91         cout<<"Case "<<cas++<<": ";
92         if(flag)cout<<"Not always an integer"<<endl;
93         else cout<<"Always an integer"<<endl;
94         s.clear();
95     }
96     return 0;
97 }
View Code

原文地址:https://www.cnblogs.com/fraud/p/4103372.html