北大ACM1006——Biorhythms~~中国剩余定理

中国剩余定理,百度一下,就有它的定义与证明。

这里我就讲一个例子就好了。

题目的意思就是给你p,e,i,d。(n + d)% 23 = p,(n + d) % 28 = e,(n + d) % 33 = i。求最小n。

将n+d看成一个整体,m =n + d。

要求m:

先使 28 * 33 * a % 23 = 1,求出a,x = 28 * 33 * a;

使 23 * 33 * b % 28 = 1,求出b,y = 23 * 33 * b;

使23 * 28 * c % 33 = 1,求出c,z = 23* 28 * c;

m = (p*x + e*y + i*z)% (23,28,33)的最小公倍数。

先求出x ,y,z。

<span style="white-space:pre">	</span>int x, y, z;
	int a = 1;
	while((28 * 33 * a % 23) != 1)
	{
		a++;
	}
	x = 28 * 33 * a;
	a = 1;
	while((23 * 33 * a % 28) != 1)
	{
		a++;
	}
	y = 23 * 33 * a;
	a = 1;
	while((23 * 28 * a % 33) != 1)
	{
		a++;
	}
	z = 23 * 28 * a;
	cout << x << ' ' << y << ' ' << z << endl;

求出的x = 5544,y = 14421,z = 1288.

k = lcm(23, 28,33),k = 21252

下面的是AC的代码:

#include <iostream>
using namespace std;

int main()
{
	int p, e, i, d, count = 1;
	int k = 21252;
	int x = 5544, y = 14421, z = 1288;
	while(cin >> p >> e >> i >> d)
	{
		if(p == -1 && e == -1 && i == -1 && d == -1)
			break;
		int m = (p * x + y * e + z * i) % k;
		if(m - d == 0)
			m = 21252;
		else if(m - d < 0)
		{
			m = m - d;
			m += 21252;
		}
		else
			m -= d;
		cout << "Case " << count++ << ": the next triple peak occurs in " << m << " days." << endl;
	}
	return 0;
}

刚学到中国剩余定理,还要继续努力加油!!~~~

时间: 2024-07-31 13:03:45

北大ACM1006——Biorhythms~~中国剩余定理的相关文章

POJ 1006:Biorhythms 中国剩余定理

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 121194   Accepted: 38157 Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical,

CSUOJ 1858 Biorhythms 中国剩余定理

1858: Biorhythms Submit Page   Summary   Time Limit: 1 Sec     Memory Limit: 256 Mb     Submitted: 69     Solved: 37 Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycl

数论E - Biorhythms(中国剩余定理,一水)

E - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the

转载----POJ 1006 中国剩余定理

本文为转载,源地址:   http://blog.csdn.net/dongfengkuayue/article/details/6461298 POJ 1006   Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 78980   Accepted: 23740 Description Some people believe that there are three cycles in a perso

gcd,扩展欧几里得,中国剩余定理

1.gcd: int gcd(int a,int b){ return b==0?a:gcd(b,a%b); } 2.中国剩余定理: 题目:学生A依次给n个整数a[],学生B相应给n个正整数m[]且两两互素,老师提出问题:有一正整数ans,对于每一对数,都有:(ans-a[i])mod m[i]=0.求此数最小为多少. 输入样例: 1 10 2 3 1 2 3 2 3 5 8 1 2 3 4 5 6 7 8 97 89 67 61 59 53 47 88 12 1 2 3 4 5 6 7 8 9

HDU 1573 X问题 中国剩余定理

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1573 题意:求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], -, X mod a[i] = b[i], - (0 < a[i] <= 10). 思路:中国剩余定理的模板题,如果找不到这样的数或者最小的X大于N,输出零. 代码: #include <iostream> #include

同余 模算术 中国剩余定理

相关知识点: 1.a≡b(modc),a,b关于模c同余  ,即a modc=b mod c , 等价于a%c=b 2.如果a,b互质(a,b)=1,则可得a关于模b的逆 ax≡1(modb) 3.关于余数的定理: 定理1 :如果被除数加上(或减去)除数的整数倍,除数不变,则余数不变. 定理2 :如果被除数扩大(或缩小)几倍,除数不变,则余数也扩大(或缩小)同样的倍数. 定理3: 如果整数a除以自然数b(b≠0),余数r仍不小于b,则r除以b的余数等于a除以b所得余数.(余数和被除数关于除数同余

hihocode 九十七周 中国剩余定理

题目1 : 数论六·模线性方程组 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:今天我听到一个挺有意思的故事! 小Hi:什么故事啊? 小Ho:说秦末,刘邦的将军韩信带领1500名士兵经历了一场战斗,战死四百余人.韩信为了清点人数让士兵站成三人一排,多出来两人:站成五人一排,多出来四人:站成七人一排,多出来六人.韩信立刻就知道了剩余人数为1049人. 小Hi:韩信点兵嘛,这个故事很有名的. 小Ho:我觉得这里面一定有什么巧妙的计算方法!不然韩信不可能这么快计

POJ 1006 中国剩余定理

[题意]: 给定p,e,i,d,求解 (x + d) % 23 = p (x + d) % 28 = e(x + d) % 33 = i x最小正整数值 [知识点]: 中国剩余定理 [题解]: 典型的 xmodmi = ai模型,其中mi间两两互素.但该题式子较少,也可以直接自己化简带入值. [代码]: 1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <ctime> 5 #inc