程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> UVA - 1069 Always an integer (模擬)

UVA - 1069 Always an integer (模擬)

編輯:C++入門知識

UVA - 1069 Always an integer (模擬)


Description

Download as PDF

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 ofn people? Into how many regions can you divide a circular disk by connectingn points on its boundary with one another? How many cubes are in a pyramid with square layers ranging from1×1 to n×n cubes?

\epsfbox{p4119.eps}

Many questions like these have answers that can be reduced to simple polynomials inn. 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 isn(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 andD 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:

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, thenCn$\scriptstyle \wedge$E is expressed asCn, unless C is 1 or -1. In those instances,Cn$\scriptstyle \wedge$E is expressed asn or - n. C is an integer. If C is 1 or -1 andE is not 0 or 1, then the Cn$\scriptstyle \wedge$E will appear as n$\scriptstyle \wedge$E or- n$\scriptstyle \wedge$E.Only non-negative C values that are not part of the first term in the polynomial are preceded by +.Exponents in consecutive terms are strictly decreasing. 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 integern. 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/D的多項式,判斷它是否在所有正整數取到整數值
思路:劉汝佳入門經典的例題,分析太長,直接給結論吧:P(1),P(2)..P(k+1)都是D的倍數,最後帶入n檢查就行了,n的范圍是1-k+1
#include 
#include 
#include 
#include 
typedef long long ll;
using namespace std;
const int maxn = 110;

int c[maxn], e[maxn], p;
int tot, len, l, tmp;
char str[10001];
int flag;

ll pow_mod(ll a, ll p, ll n) {
	ll tmp = 1;
	while (p) {
		if (p & 1)
			tmp = (tmp * a) % n;
		p >>= 1;
		a = (a * a) % n;
	}
	return tmp;
}
void cal(int s, int a, int b) {
	int k, cs = 0, es = 0;
	for (k = a; k <= b; k++)
		if (str[k] == 'n')
			break;
	for (int i = a; i < k; i++)
		cs = cs * 10 + str[i] - '0';
	if (cs == 0)
		cs = s;
	else cs *= s;
	for (int i = k+2; i <= b; i++)
		es = es * 10 + str[i] - '0';
	if (k <= b && es == 0)
		es = 1;
	c[tot] = cs;
	e[tot++] = es;
}

int main() {
	int cas = 1;
	while (scanf("%s", str) && str[0] != '.') {
		len = strlen(str);
		l = tot = 0;
		if (str[1] == '-')
			l = 2;
		else l = 1;
		for (int i = l; i < len; i++) {
			if (str[i] == '+' || str[i] == '-' || str[i] == ')') {
				if (str[l-1] == '-')
					cal(-1, l, i-1);
				else cal(1, l, i-1);
				l = i+1;
			}
			if (str[i] == '/') {
				p = 0;
				for (int j = i+1; j < len; j++) 
					p = p*10 + str[j] - '0';
				break;	
			}
		}
		
		flag = 1;
		for (int i = 1; i < 200; i++) {
			tmp = 0;
			for (int j = 0; j < tot; j++)
					tmp = (tmp + (c[j]%p)*pow_mod(i, e[j], p)) % p;
			if (tmp) {
				flag = 0;
				break;
			}
		}

		if (flag) 
			printf("Case %d: Always an integer\n", cas++);
		else printf("Case %d: Not always an integer\n", cas++);
	}
	return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved