HDU 1452 Happy 2004

本文详细讲解了如何通过数学公式和编程实现求解2004的n次方的因子和对29取模的操作,包括分解素因子、使用高斯求和公式及模运算优化等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:求2004的n次方的因子和mod29

这里写图片描述
//这里讲述的很详细http://www.cppblog.com/menjitianya/archive/2015/12/02/212395.html
x的因子和为 sigma((pi(e1+1)-1)/(pi-1)) pi为x的素因子
x^n的因子和就是 sigma((pi(n*e1+1)-1)/(pi-1))
2004的素因子有2 3 167
2004=(2^2)* (3^1)* (167^1)
2004^n=(2^(2*n)) (3^n) (167^n)

2004^n的因子和就是 (2^(2*n+1)-1) * (3^(n+1)-1)/2 * (167^(n+1)-1)/166 mod 29
->(2^(2*n+1)-1) * (3^(n+1)-1)/2 * (22^(n+1)-1)/21 mod 29 //(22=167%29)
(a*b)/c %M= a%M* b%M * inv(c)
inv(c)为c的逆 逆可以通过扩展欧几里得求得
所以上式就可以转化为
(2^(2*n+1)-1) * (3^(n+1)-1) * inv(2) * (22^(n+1)-1) *inv(21) mod 29
->(2^(2*n+1)-1) * (3^(n+1)-1) * 15 * (22^(n+1)-1) *18 mod 29

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x&(-x))
typedef long long LL;
#define maxn 10005
const int inf=(1<<28)-1;
const int mod=29;
int quick_pow(int a,int b)
{
    int tmp=a,ans=1;
    while(b)
    {
        if(b&1) ans=(ans*tmp)%mod;
        tmp=(tmp*tmp)%mod;
        b/=2;
    }
    return ans;
}
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        int ans=1;
        ans*=(quick_pow(2,2*n+1)-1)%mod;
        ans*=((quick_pow(3,n+1)-1)*15)%mod;
        ans*=((quick_pow(22,n+1)-1)*18)%mod;
        printf("%d\n",ans%mod);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值