zoukankan      html  css  js  c++  java
  • 数学--数论--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)=2004xF(x)=2004^x的因子和 因为这是个积性函数,则有f(N)=i=1nf(qiqi)Ni=1nqiqif(N)=prod_{i=1}^nf(q_i ^{q_i}) 其中 N可以表示为prod_{i=1}^nq_i ^{q_i}
    f2004n=f(2(2n))f3n)f167n=22n+113n+11/2167n+11/166f(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

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

    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();
    	}
    	
    }
    
    
  • 相关阅读:
    c# 国际化国家代码
    转:Oracle Connect By用法
    函数关系: 单射 满射 双射
    python 乱码
    2010年下半年全国计算机技术与软件专业技术资格(水平)考试试题下载
    HDU1068 Girls and Boys 最大独立子集
    HDU1151 Air Raid 最小简单路径覆盖
    POJ2411 Mondriaan's Dream 状态压缩+DP
    HDU1556 方格取数(1) 状态压缩+dp
    POJ2239 Selecting Courses 二分匹配
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798503.html
Copyright © 2011-2022 走看看