hdu 1452 (积性函数+因子和+乘法逆元)

题意:
给一个数x,求2004^x mod 29 等于多少。

解析:
用到的知识很琐碎,但是分析完了以后很简单,重点在分析。

首先,积性函数就是f(a * b) =  f(a) * f(b)
因子和和因子个数函数都是积性函数。

然后是因子和的一个定理,若p为素数:
f(p ^ n ) = 1 + p ^ 1 + p ^ 2 + p ^ 3 + ... p ^ n = ( p ^ (n + 1) - 1 ) / (p - 1).

最后是乘法逆元,eg,4关于1模的7乘法逆元x的定义是 4 * x = 1 (mod 7)。
除以一个数取模的结果和乘以除数的乘法逆元取模的结果是一样的。

这题:f(2004 ^ n ) = f((2 ^ (2n) )*(3  ^ n )*(167 ^ n ))
由于167 = 22 (mod 29) , 所以:
f(2004 ^ n )= f(2 ^ (2n) ) *  f(3 ^ n ) * f(22 ^ n).
令:a = f(2 ^ (2n) )= (2 ^ (2n+1) -1)/ 1
      b = f(3 ^ n )     = (3 ^ (n + 1) - 1)/ 2
      c  = f(22 ^ n)   =  (22 ^(n + 1) - 1) / 21
由于除法模等于乘乘法逆元取模,而inv(3 - 1) = 15,inv(22 - 1) = 18.
搞搞就好了。

代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long

using namespace std;

const int mod = 29;

LL pow_mod(LL a, LL n)
{
    if (n == 0)
        return 1;
    LL x = pow_mod(a, n >> 1);
    LL res = x * x % mod;
    if (n & 1)
        res = res * a % mod;
    return res;
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    LL n;
    while (scanf("%I64d", &n) && n)
    {
        LL a = pow_mod(2, 2 * n + 1) - 1;
        LL b = (pow_mod(3, n + 1) - 1) * 15 % 29;
        LL c = (pow_mod(22, n + 1) - 1) * 18 % 29;
        printf("%lld\n", a * b * c % 29);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值