UVA 1069 - Always an integer(数论)

1069 - Always an integer

题意:给定一个多项式,推断是否总是整数

思路:LRJ大白上的例题,上面给出了证明,仅仅要1到k + 1(k为最高次数)带入方程都是整数,那么整个方程就是整数,处理出字符串后,然后过程用高速幂计算,推断最后答案是否为0,看是否全都满足是整数。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

char str[105];

struct X {
	long long a, k;
} x[105];

long long mu, Max;
int xn;

void build() {
	mu = Max = 0; xn = 0;
	long long s = 0, a = 0, flag = 1, k = 0;
	int len = strlen(str);
	for (int i = 0; i < len; i++) {
		if (str[i] >= '0' && str[i] <= '9') {
			if (s == 0) a = a * 10 + str[i] - '0';
			if (s == 1) k = k * 10 + str[i] - '0';
			if (s == 2) mu = mu * 10 + str[i] - '0';
  		}
  		else if (str[i] == 'n')
  			s = 1;
		else if (str[i] == '/')
			s = 2;
		else if (str[i] == '+' || str[i] == '-' || str[i] == ')') {
			if (s >= 1) {
				if (a == 0) a = 1;
				if (k == 0) k = 1;
   			}
   			Max = max(Max, k);
   			x[xn].a = a * flag; x[xn].k = k; xn++;
   			if (str[i] == '-') flag = -1;
   			else if (str[i] == '+') flag = 1;
   			a = k = s = 0;
  		}
 	}
}

long long pow_mod(long long x, long long k) {
	long long ans = 1;
	while (k) {
		if (k&1) ans = ans * x % mu;
  		x = x * x % mu;
  		k >>= 1;
 	}
 	return ans;
}

bool judge() {
	for (long long i = 0; i <= Max + 1; i++) {
		long long ans = 0;
  		for (int j = 0; j < xn; j++) {
			ans = (ans + x[j].a * pow_mod(i, x[j].k)) % mu;
  		}
  		if (ans) return false;
 	}
 	return true;
}

int main() {
	int cas = 0;
	while (~scanf("%s", str) && str[0] != '.') {
		build();
		printf("Case %d: %s\n", ++cas, judge()?"Always an integer":"Not always an integer");
 	}
	return 0;
}
时间: 2024-08-06 12:08:45

UVA 1069 - Always an integer(数论)的相关文章

Uva 1069 Always an Integer ( 数学 )

Uva 1069 Always an Integer ( 数学 ) #include <cstdio> #include <cstring> #include <algorithm> #include <cctype> using namespace std; typedef long long LL; #define MAXN 105 #define CLR( a, b ) memset( a, b, sizeof(a) ) LL c[ MAXN ], d

uva 1069 - Always an integer(数学+暴力)

题目链接:uva 1069 - Always an integer 题目大意:给出一个多次多项式,问说是否对于任意正整数n来说结构均为整数. 解题思路:首先处理出字符串,然后枚举n从1到k+1判断即可,k为多项式中出现过的最大幂数指. P为多项式,d为除数,k为多项式中最大的次数 当k=0时,P中不存在n变量,所以直接计算判断即可 当k=1时,P是一次多项式,那么P(n+1)?P(n)=a,a为常数,也就是说P(i)为等差数列,如果首项P(1)和公差P(2)-P(1)为d的倍数即可,等价与判断P

UVA - 1069 Always an integer (模拟)

Description 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

uva 10104 Euclid Problem (数论-扩展欧几里德)

 Euclid Problem  The Problem From Euclid it is known that for any positive integers A and B there exist such integers X and Y that AX+BY=D, where D is the greatest common divisor of A and B. The problem is to find for given A and B corresponding X, Y

UVA 1341 - Different Digits(数论)

UVA 1341 - Different Digits 题目链接 题意:给定一个正整数n,求一个kn使得kn上用的数字最少,如果相同,则输出值最小的 思路: 首先利用鸽笼原理证明最多需要2个数字去组成 设一个数字k,组成k,kk,kkk,kkkk... %n之后余数必然在0 - (n - 1)之间,所以必然能选出两个余数相等的数字相减为0,这个数字就是由0和k组成的. 因此只要考虑一个数字和两个数字的情况,去bfs,记忆化余数,因为余数重复必然形成周期了 代码: #include <stdio.

UVA 10693 10693 - Traffic Volume(数论)

题目链接:10693 - Traffic Volume 根据物理知识, 车经过的时间等于,距离/速度,所以可以列出公式t = (l + d)/v,v/2f + d/v,只有当v / 2f = d/v时,时间最小,v = sqrt(2df),之后时间也能算了. #include <stdio.h> #include <string.h> #include <math.h> double l, f; int main() { while (~scanf("%lf%

UVA 618 - Doing Windows(数论)

题目链接:618 - Doing Windows 题意:给定一个大小不能变的屏幕,和四个大小可以变的窗口,变化要保持长宽比,问这四个窗口能不能调整后全部放下正好填满屏幕,不能重叠 思路:情况一共就几种:4个叠一起,3个叠一起+一个,2个和2个,一个和两个叠一起在一个,把这几种情况全判断了就可以了,判断过程利用gcd,lcm可以求边长. 代码: #include <stdio.h> #include <string.h> long long gcd(long long a, long

UVA 1426 - Discrete Square Roots(数论)

UVA 1426 - Discrete Square Roots 题目链接 题意:给定X, N, R,要求r2≡x (mod n) (1 <= r < n)的所有解,R为一个已知解 思路: r2≡x (mod n)=>r2+k1n=x 已知一个r!,带入两式相减得 r2?r12=kn => (r+r1)(r?r1)=kn 枚举A,B,使得 A * B = n (r + r1)为A倍数 (r - r1)为B倍数 这样就可以推出 Aka?r1=Bkb+r1=r => Aka=Bk

uva 10515 - Powers Et Al.(数论)

题目链接:uva 10515 - Powers Et Al. 题目大意:给出m和n,问说mn的个数上的数是多少. 解题思路:其实只要看m的最后一位数就可以了,判断最有一位的周期,然后用n%t即可. #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn = 15; const int maxs = 105; vector<int> g