Hdu 1452 Happy 2004(除数和函数,快速幂乘(模),乘法逆元)


Problem Description

Considera positive integer X,and let S be the sum of all positive integer divisors of2004^X. Your job is to determine S modulo 29 (the rest of the division of S by29).

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 29is equal to 6.

Input

Theinput consists of several test cases. Each test case contains a line with theinteger X (1 <= X <= 10000000).

A test case of X = 0 indicates the end of input, and should not be processed.

Output

Foreach test case, in a separate line, please output the result of S modulo 29.

Sample Input

1

10000

0

Sample Output

6

10

/***************************

解题思路:

参考大神的:http://www.cnblogs.com/372465774y/archive/2012/10/22/2733977.html

体重主要用到除数和函数。

除数和函数 :F(n) 求n的约数的和 ( 约数大于等于1 小于n )

除数和函数是一个积性函数,满足性质 :当m , n 互质时, f(m*n) = f(m) * f(n)

如果 p 是一个素数,则 f(p^n) = 1 + p + p^2 +p^3 +p^4 + .... + p^(n-1) + p^n = (p^(n+1) -1)/p-1 (等比数列求和)

则题目中 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

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

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

具体参考:http://baike.baidu.com/link?url=pcN2WyxgeFP9isdQxd9bTobeiRH3MnXcrdIwHh7jCBsYkVyTfFhF5QiS-d8-HgNgslVb334pgqkClTiIp359Xa

然后还要用到 快速幂模:转换为位运算,这题要用这个,一般的会超时,具体看代码吧。

*************************/

Code:

#include<stdio.h>
using namespace std;
int Mod(int a,int b)//  快速幂模函数
{
    int t = 1;
    while(b)
    {
        if(b&1)
            t = t*a%29;
        b>>=1;
        a = a*a%29;
    }
    return t;
}
int main()
{

    int n,a,b,c;
    while(scanf("%d",&n)&&n)
    {
        a=(Mod(2,2*n+1)-1);
        b=(Mod(3,n+1)-1)*15;
        c=(Mod(22,n+1)-1)*18;
        printf("%d\n",a*b*c%29);
    }
    return 0;
}



优质内容筛选与推荐>>
1、SOUI中启用拖文件
2、如何在状态栏显示当前鼠标位置
3、Oracle中查看所有表和字段
4、jquery ajax 样例
5、用命令行执行php脚本输出乱码


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号