Problem B : Always an Integer

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?


                   TFigure 1:T If we connect six points on the boundary of a circle, at most 31 regions are created.

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^2-n)/2. The answer to the second is (n^4-6n^3+23n^2-18n+24)/24. The answer to the third is n(n+1)(2n+1)/6, or (2n^3+3n^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^E, where the coefficient C and the exponent E satisfy the following conditions:
1. E is an integer satisfying 0 ≤ E ≤ 100. If E is 0, then Cn^E is expressed as C. If E is 1, then Cn^E is expressed as Cn, unless C is 1 or -1. In those instances, Cn^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^E will appear as n^E or -n^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 TAlways an integerT if the test casepolynomial evaluates to an integer for every positive integer n. Print TNot always an integerT 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
.

Output for the Sample Input
Case 1: Always an integer
Case 2: Always an integer
Case 3: Not always an integer

题目大概的意思是说:给定一个关于n的p次多项式,问该多项式是否为整值多项式。
根据定理:n次多项式f(n)是整值多项式当且仅当f(n)至少在n+1个连续的整数上都取整值。
只用将0-MAXPOW(取101)依次代入多项式的分子,并对分母d取模,判断是否都为0即可。
至于为什么要取MAXPOW,而不是多项式f(n)的最大的次数max{pi}:为了使问题一般化,我们可以讲所有的多项式都看成是MAXPOW次的,只不过当次数p>max{pi}时,其对应的系数ci全部为0,并不妨碍问题的解决。这样一来,就不需要再额外求出f(n)的最大次数max{pi},使程序得到简化。

399645  2009-04-23 07:44:07 Accepted 0.066 Minimum 19193  C++ 4119 - Always an integer
 1 #include <iostream>
 2 using namespace std;
 3 
 4 const int MAXPOW = 101;
 5 int c[MAXPOW],d;
 6 char ch;
 7 
 8 int calculate(long long n){
 9     int i;
10     long long ans=0;
11     for(i=MAXPOW;i>=0;i--)
12         ans=(ans*n+c[i])%d;
13     return (int)ans;
14 }
15 bool judge(){
16     int i;
17     for(i=0;i<=MAXPOW;i++)
18         if(calculate(i)) return false;
19     return true;
20 }
21 int main(){
22     int end,ca=1,sign,value,pow;
23     while(true){
24         ch=getchar();
25         if(ch=='.'break;
26         memset(c,0,sizeof(c));
27         while(true){
28             end=0,scanf(")%n",&end);
29             if(end) break;
30             scanf("+");
31             sign=0,value=1,scanf("-%n",&sign);
32             scanf("%d",&value);
33             if(sign) value=-value;
34             scanf("%nn%n^%n",&pow,&pow,&pow);
35             if(pow>1) scanf("%d",&pow);
36             c[pow]+=value;
37         }
38         scanf("/%d",&d);
39         getchar();
40         printf("Case %d: ",ca++);
41         puts(judge() ? "Always an integer" : "Not always an integer");
42     }
43     return 0;
44 }

posted on 2009-04-23 12:51 极限定律 阅读(1851) 评论(0)  编辑 收藏 引用 所属分类: ACM-ICPC World Final 2008题解


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2009年5月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(10)

随笔分类

随笔档案

友情链接

搜索

最新评论

阅读排行榜

评论排行榜