POJ 1006 数论简单题

博客详细介绍了如何不用中国剩余定理,而是通过简单模拟解决POJ 1006数论问题的方法。
摘要由CSDN通过智能技术生成

不用中国剩余定理的知识,直接简单模拟

#include <iostream>

using namespace std;

const int pc = 23, ep = 28, ip = 33;
int p, e, i, d;

bool Judge(int ans)
{
    if( (ans-p)%pc != 0 )
       return false;
    if( (ans-e)%ep != 0 )
	return false;
    if( (ans-i)%ip != 0 )
	return false;
    return true;
}
int main()
{
    int iCase = 0;
    while( cin >> p >> e >> i >> d && (~p || ~e || ~i || ~d) )
    {
	++iCase;
	p = p%23;
	e = e%28;
	i = i%33;
	int ans = d + 1;
	while( true )
	{
	    if( Judge( ans ) )
	    {
		cout << "Case " << iCase << ": the next triple peak occurs in ";
		cout << ans - d << " days." << endl;
		break;
	    }
	    ++ans;
	}
    }
    return 0;
}
上面的简单模拟,差一点就超时了,估计在uva上面会超时的,利用中国剩余定理,o(1)解决战斗!

#include <iostream>
#include <cstdio>

using namespace std;

const int PC = 23, EC = 28, IC = 33;
const int PEI = PC*EC*IC;
int n1 = EC*IC, n2 = PC*IC, n3 = PC*EC;

void Init()
{
    int t = n1;
    while( n1%PC != 1 )
	n1 += t;
    t = n2;
    while( n2%EC != 1 )
	n2 += t;
    t = n3;
    while( n3%IC != 1 )
	n3 += t;
}

int main()
{
    int iCase = 0;
    int p, e, i, d;
    Init();
    while( scanf("%d%d%d%d", &p, &e, &i, &d) && (~p || ~e || ~i || ~d) )
    {
	++iCase;
	p %= PC;
	e %= EC;
	i %= IC;
	int ans = (n1*p + n2*e + n3*i)%PEI;
	if( ans <= d )
	    ans += PEI;
	ans -= d;
	cout << "Case " << iCase << ": the next triple peak occurs in ";
	cout << ans << " days." << endl;
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值