您的位置:首页 > 移动开发

HDU 1452 Happy 2004 求2004^n的所有因子和 积性函数应用

2015-05-12 12:25 459 查看
题目链接:点击打开链接

百度个题解:点击打开链接

6的因子是1,2,3,6; 6的因子和是 s(6)=1+2+3+6=12;

20的因子是1,2,4,5,10,20; 20的因子和是 s(20)=1+2+4+5+10+20=42;

2的因子是1,2; 2的因子和是 s(2)=1+2=3;

3的因子是1,3; 3的因子和是 s(3)=1+3=4;

4的因子和是 s(4)=1+2+4=7;

5的因子和是 s(5)=1+5=6;

s(6)=s(2)*s(3)=3*4=12;

s(20)=s(4)*s(5)=7*6=42;

这是巧合吗?

再看 s(50)= 1+2+5+10+25+50=93=3*31=s(2)*s(25),s(25)=1+5+25=31.

这在数论中叫积性函数,当gcd(a,b)=1时 s(a*b)=s(a)*s(b);

如果p是素数

s(p^n)=1+p+p^2+...+p^n= (p^(n+1)-1) /(p-1) (1)

例 hdu1452 Happy 2004

计算 因子和 s(2004^X) mod 29 ,

2004=2^2 *3 *167

s(2004^X) ) = (s(2^2X))) * (s(3^X))) * (s(167^X)))

167)=22;

s(2004^X) ) = (s(2^2X))) * (s(3^X))) * (s(22^X)))

a=s(2^2X)=(2^(2X+1)-1) //根据 (1)

b=s(3^X)= (3^(X+1)-1)/2 //根据 (1)

c=s(22^X)= (22^(X+1)-1)/21 //根据 (1)

%运算法则 1. (a*b) %p= ( a%p)
*(b%p)

%运算法则 2. (a/b) %p= ( a *b^(-1)%p)

b^(-1)是 b的逆元素 (%p)

2的逆元素是15 ())
,因为2*15=30 % 29=1 % 29

21的逆元素是18 ())
,因为21*18=378% 29 =1 % 29

因此

a=(powi(2,2*x+1,29)-1)% 29;

b=(powi(3,x+1,29)-1)*15 % 29;

c=(powi(22,x+1,29)-1)*18 % 29;

ans=(a*b)% 29*c % 29;

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <vector>
#include <string>
#include <time.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <stack>
#include <set>
#include <map>
const int inf = 1e8;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T>
inline bool rd(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
template <class T>
inline void pt(T x) {
	if (x <0) { putchar('-'); x = -x; }
	if (x>9) pt(x / 10);
	putchar(x % 10 + '0');
}
using namespace std;
const int mod = 29;
int Pow(int x, int y){
	int ans = 1;
	while (y){
		if (y & 1)ans = ans*x%mod;
		y >>= 1;
		x = x*x%mod;
	}
	return ans;
}
int n;//2004= 2*2*3*167
int gao(int x, int y){
	int ans = Pow(x, y + 1) - 1;
	while (ans % (x - 1))ans += mod;
	ans /= x - 1;
	return ans%mod;
}
int main(){
	while (cin>>n, n){
		int ans = gao(2, 2 * n) * gao(3, n) * gao(167, n);
		cout << ans % mod<< endl;
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: