ACM 数论 HDU 1452 Happy 2004 积性函数

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
  
  
1 10000 0
 

Sample Output
  
  
6 10
 

Source
 

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;
 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值