POJ1845------Sumdiv

本文介绍一种算法,用于求解一个数的幂次的所有因数之和,并对其结果取9901的模。该算法首先进行质因数分解,然后利用数学方法计算每个质因数对应的等比数列之和。

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

Sumdiv
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 16189 Accepted: 4022

题意:求n的m次幂的所有因数的和对9901取余;

思路:各种数学方法:

1: 对A进行素因子分解得

     A= p1^a1 * p2^a2 * p3^a3 *...* pn^an.
  故 A^B = p1^(a1*B) * p2^(a2*B)*...* pn^(an*B);


2:A^B的所有约数之和为:

     sum= [1+p1+p1^2+...+p1^(a1*B)] * [1+p2+p2^2+...+p2^(a2*B)] *...*[1+pn+pn^2+...+pn^(an*B)].

  如 200 = 2^3 * 5^2:  sum(200) = [1 + 2 + 4 + 8] * [1 + 5 + 25].


3: 求等比数列1+pi+pi^2+pi^3+...+pi^n可以由递归形式的二分求得:(模运算不能用等比数列和公式!)
   若n为奇数,一共有偶数项,则:
     1 + p + p^2 + p^3 +...+ p^n

     = (1+p^(n/2+1)) + p * (1+p^(n/2+1)) +...+ p^(n/2) *(1+p^(n/2+1))
     = (1 + p + p^2 +...+ p^(n/2)) * (1 + p^(n/2+1))

   如:1 + p +p^2 + p^3 + p^4 + p^5 = (1 + p +p^2) * (1 + p^3)
   若n为偶数,一共有奇数项,则:
     1 + p + p^2 + p^3 +...+ p^n

     = (1+p^(n/2+1)) + p * (1+p^(n/2+1)) +...+ p^(n/2-1) * (1+p^(n/2+1))+ p^(n/2)
      =(1 + p + p^2 +...+ p^(n/2-1)) * (1+p^(n/2+1)) + p^(n/2);

  如:1 + p + p^2 + p^3 +p^4 = (1 + p) * (1 + p^3) + p^2

以上来自:http://blog.sina.com.cn/s/blog_6635898a0100omcn.html;


Description

Consider two natural numbers A and B. Let S be the sum of all natural divisors of A^B. Determine S modulo 9901 (the rest of the division of S by 9901).

Input

The only line contains the two natural numbers A and B, (0 <= A,B <= 50000000)separated by blanks.

Output

The only line of the output will contain S modulo 9901.

Sample Input

2 3

Sample Output

15

Hint

2^3 = 8.
The natural divisors of 8 are: 1,2,4,8. Their sum is 15.
15 modulo 9901 is 15 (that should be output).

Source

Romania OI 2002

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<cctype>
#include<map>
#include<set>
#include<vector>
#define LL long long
using namespace std;
const int  MOD= 9901;
LL pow(LL u,LL n)
{
    LL ans=1;
    while(n)
    {
        if(n&1)
            ans=(ans*u)%MOD;
        u=(u*u)%MOD;
        n>>=1;
    }
    return ans;
}
LL sum(LL u,LL n)
{
    if(n==0)
        return 1;
    if(n&1)
        return ( (1+pow(u,n/2+1))*sum(u,n/2))%MOD;
    else
        return((1+pow(u,n/2+1))*sum(u,n/2-1)+pow(u,n/2))%MOD;
}
int main()
{
     LL n,m;
     LL ans;
     while(~scanf("%lld%lld",&n,&m))
     {
         ans=1;
         LL u,v;
         for(LL i=2;i*i<=n;i++)
         {
                    v=0;
                 while(n%i==0)
                 {
                     v++;
                     n/=i;
                 }

             ans=(ans*sum(i,v*m))%MOD;
         }
         if(n!=1)
            ans=(ans*sum(n,m))%MOD;
         printf("%lld\n",ans);
     }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值