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

\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$Eis 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








#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char input[2000];
bool ok;
int i,j,position,N,cases;
long long D,calc,T;
long long A[110];

long long get_num()
{
	long long tmp;
	tmp=0;
	while('0'<=input[position] && input[position]<='9')
	{
		tmp=tmp*10+input[position]-'0';
		position++;
	}
	return tmp;
}

bool init()
{
	int sign;
	long long C,E;
	gets(input);
	if(input[0]=='.')
		return false;
	memset(A,0,sizeof(A));
	position=1;
	while(input[position]!=')')
	{
		if(input[position]=='-')
		{
			sign=-1;
			position++;
		}
		else if(input[position]=='+')
		{
			sign=1;
			position++;
		}
		else
			sign=1;
		if(input[position]!='n')
			C=get_num();
		else
			C=1;
		C*=sign;
		if(input[position]!='n')
			E=0;
		else
		{
			position++;
			if(input[position]!='^')
				E=1;
			else
			{
				position++;
				E=get_num();
			}
		}
		A[E]=C;
	}
	position+=2;
	D=get_num();
	return true;
}

void work()
{
	for(N=100;A[N]==0;N--);
		ok=true;
	for(i=1;i<=N+1;i++)
	{
		calc=0;
		T=1;
		for(j=0;j<=N;j++)
		{
			calc=(calc + (T*A[j])%D)%D;
			T=(T*i)%D;
		}
		if(calc!=0)
			ok=false;
	}
	if(ok)
		printf("Case %d: Always an integer\n",++cases);
	else
		printf("Case %d: Not always an integer\n",++cases);
}

int main()
{
	cases=0;
	while(init())
		work();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值