Happy 2004

来源:互联网 发布:童鞋淘宝网 编辑:程序博客网 时间:2024/05/19 19:14
链接: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 100010ll 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)的逆元为15b *= ((quickpow(22, a + 1) - 1) * 18) % 29;//167与22对29同余,166的逆元为18b %= 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 100010ll 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
原创粉丝点击