ACM 数论 HDU 1452 Happy 2004 积性函数

来源:互联网 发布:淘宝网玩具店 编辑:程序博客网 时间:2024/05/09 01:20
Problem Description
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
1100000
 

Sample Output
610
 

Source
ACM暑期集训队练习赛(六)
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1695 1573 1788 1060 1370 



比较基础的题,作为新手个人感觉  先看答案再做. 不知道方法,做再久也做不出来的.还不如先看代码.

这道题求2014的x次方的所有因子的和,输出%29后的值.

暴力求解 是绝对不可能的,我们要做的是对2004^x的因子和%29这个公式进行化简.

下面就是套路了,满满的套路,一定要记住.

对于求一个数的因子和,我们要做的是分解成素因数的乘积.

2004^x=(2*2*3*167)^x

f(2004^x)=f(2^x)*f(2^x)*f(3^x)*f(167^x);

接下来还是套路!ps:这个充满套路的世界 

p^k=1+p^1+p^2+p^3+....+p^k;积性方程!

所以f(2004^x)可以继续化简;

最 后得到  2^(2X+1)-1) * (3^(X+1)-1)* (22^(X+1)-1)/334

之后%29;

加减乘不用说.

主要 是那 个334是除.对于除的,我们取倒数.就是说334%29=9. 我们就*9,而不是/9;

(2^(2X+1)-1) * (3^(X+1)-1)* (22^(X+1)-1)*9)%29

公式出来了,代码就简单了




#include<stdio.h>


 int ans,x;


int hanshu(int m,int n){
     int ans=1;
     while(n){
         if(n&1) ans=(ans*m)%29;
         m=(m*m)%29;
         n>>=1;
    }
     return ans%29;
 }


 int main(){
    while(scanf("%d",&x),x){
         int ans=0;
        ans=(hanshu(2,2*x+1)-1)%29;
         ans=ans%29*(hanshu(3,x+1)-1)*15%29;
        ans=ans%29*(hanshu(22,x+1)-1)*18%29;
         printf("%d\n",ans%29);
    }
     return 0;
 }

0 0
原创粉丝点击