数学--数论--Hdu 1452 Happy 2004(积性函数性质+和函数公式+快速模幂+乘法逆元)

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.
Input
The input consists of several test cases. Each test case contains a line with the integer X (1 <= X <= 10000000).

A test case of X = 0 indicates the end of input, and should not be processed.
Output
For each test case, in a separate line, please output the result of S modulo 29.
Sample Input
1
10000
0
Sample Output
6
10

F ( x ) = 200 4 x 的 因 子 和 F(x)=2004^x的因子和 F(x)=2004x 因为这是个积性函数,则有 f ( N ) = ∏ i = 1 n f ( q i q i ) 其 中 N 可 以 表 示 为 ∏ i = 1 n q i q i f(N)=\prod_{i=1}^nf(q_i ^{q_i}) 其中 N可以表示为\prod_{i=1}^nq_i ^{q_i} f(N)=i=1nf(qiqi)Ni=1nqiqi
f ( 200 4 n ) = f ( 2 ( 2 ∗ n ) ) ∗ f ( 3 n ) ∗ f ( 16 7 n ) = ( 2 ( 2 ∗ n + 1 ) − 1 ) ∗ ( 3 ( n + 1 ) − 1 ) / 2 ∗ ( 16 7 ( n + 1 ) − 1 ) / 166 f(2004 ^ n)= f(2 ^{(2 * n)})* f(3 ^ n)* f(167 ^ n) =(2 ^{(2 * n + 1)}-1)*(3 ^{(n + 1)}-1)/ 2 *(167 ^{(n + 1)}-1)/ 166 f2004n=f(2(2n))f3n)f167n=22n+113n+11/2167n+11/166

用到乘法逆元:(同余性质)

a ^ k / d = a ^ k *(d-1)d-1即为d的逆元。3的逆元为15 167的逆元为18

JAVA C++ 没区别,最近在JAVA要考试了额,熟悉一下。

import java.util.Scanner;

public class Main {
	public static long Q_pow( long a, long p, long mod)
	{
		long ans = 1%mod;
	     while(p>0)  {
	         if(p%2==1)  ans = ans*a%mod;  //防止在对P取模前溢出
	         a = a*a%mod;
	         p >>=1;  //比除法快多了
	     } 
	     return ans;
	}
	public static void main(String[] args) {
		int n;
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			n=in.nextInt();
			if(n==0) break;
			long ans=((Q_pow(167,n+1,29)-1)*18)%29;
			ans=(ans*((Q_pow(3,n+1,29)-1)*15)%29)%29;
			ans=(ans*(Q_pow(2,2*n+1,29)-1))%29;
			System.out.println(ans);
		}
		in.close();
	}
	
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值