Happy 2004

链接:http://acm.hust.edu.cn/vjudge/problem/40652/origin

题目:Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your job is to determine S modulo 29 (the rest of the division of S by 29).

Take X = 1 for an example. The positive integer divisors of 2004^1 are 1, 2, 3, 4, 6, 12, 167, 334, 501, 668, 1002 and 2004. Therefore S = 4704 and S modulo 29 is equal to 6.

题意:求2004的n次幂的所有约数(可以被2004的n次幂除尽的数)的和对29的余数。

分析:本道题用了很多数论的知识,图转,侵删。。


另外的是一样的余数定理,加减乘都可以分配律,只有除法要变成逆元乘法,详细原理可以百度乘法逆元,下面给出一个求解逆元的代码,这里用的是扩展欧几里得求逆元.

具体的思路是2004乘方就是质因数在乘方。2004=2*2*3*167共4个质因数,分别用上面的方法计算约数和即可。注意把除法的3-1,167-1变为逆元,2-1的逆元还是1。


题解:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <functional>
#include <cmath>
#include <cctype>
#include <cfloat>
#include <climits>
#include <complex>
#include <deque>
#include <list>
#include <set>
#include <utility>
#define rt return
#define fr freopen("in.txt","r",stdin)
#define fw freopen("out.txt","w",stdout)
#define ll long long
#define ull unsigned long long
#define detie ios_base::sync_with_stdio(false);cin.tie(false);cout.tie(false)
#define pii pair<int,int>
#define lowbit(x) x&(-x)
using namespace std;
#define maxi 0x3f3f3f3f
#define MAX 100010

ll exgcd(ll a, ll b, ll &x, ll &y)//小心b为零的情况,保证输入的时候b不为零
{
	if (b == 0)
	{
		x = 1;
		y = 0;
		rt a;
	}
	ll d = exgcd(b, a%b, x, y);
	ll temp = x;
	x = y;
	y = temp - (a / b)*y;
	rt d;
}

ll quickpow(ll x, ll n)
{
	ll ans = 1;
	while (n)
	{
		if (n & 1)ans *= x;
		n /= 2;
		x *= x;
		ans %= 29;
		x %= 29;
	}
	rt ans;
}

int main()
{
	//fr;
	detie;
	ll a, b, x, y;
	while (cin >> a&&a)
	{
		b = 1;
		b *= (quickpow(2, 2 * a + 1) - 1) % 29;//等比数列求和
		b *= ((quickpow(3, a + 1) - 1) * 15) % 29;//除数(3-1)的逆元为15
		b *= ((quickpow(22, a + 1) - 1) * 18) % 29;//167与22对29同余,166的逆元为18
		b %= 29;
		cout << b << endl;
	}
	rt 0;
}

求逆元:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <functional>
#include <cmath>
#include <cctype>
#include <cfloat>
#include <climits>
#include <complex>
#include <deque>
#include <list>
#include <set>
#include <utility>
#define rt return
#define fr freopen("in.txt","r",stdin)
#define fw freopen("out.txt","w",stdout)
#define ll long long
#define ull unsigned long long
#define detie ios_base::sync_with_stdio(false);cin.tie(false);cout.tie(false)
#define pii pair<int,int>
#define lowbit(x) x&(-x)
using namespace std;
#define maxi 0x3f3f3f3f
#define MAX 100010

ll gcd(ll a, ll b)//小心b为零的情况,保证输入的时候b不为零
{
	rt (a%b) ? gcd(b, a%b) : b;
}

ll lcm(ll a, ll b)
{
	rt a / gcd(a, b)*b;
}

ll exgcd(ll a, ll b, ll &x, ll &y)//小心b为零的情况,保证输入的时候b不为零
{
	if (b == 0)
	{
		x = 1;
		y = 0;
		rt a;
	}
	ll d = exgcd(b, a%b, x, y);
	ll temp = x;
	x = y;
	y = temp - (a / b)*y;
	rt d;
}

int main()
{
	ll a,b,x, y;
	while (1)
	{
		cin >> a >> b;
		ll m=exgcd(a, b, x, y);
		if (x < 0)
			x += ((0 - x) / b + 1)*b;
		cout << x <<endl<< y << endl;
	}
	rt 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值