POJ 1845 Sumdiv (快速幂+质因数+约数和公式+同余模)

Sumdiv
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 16109 Accepted: 3992

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).  



题目大意:求约数和。

思路:本来看着挺简单,但是一个数据范围,让这道题瞬间高大上起来。

做本题需要一定的基本知识,应用定理主要有三个:

(1)   整数的唯一分解定理:

      任意正整数都有且只有一种方式写出其素因子的乘积表达式。

      A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^kn)   其中pi均为素数

(2)   约数和公式:

对于已经分解的整数A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^kn)

有A的所有因子之和为

    S = (1+p1+p1^2+p1^3+...p1^k1) * (1+p2+p2^2+p2^3+….p2^k2) * (1+p3+ p3^3+…+ p3^k3) * .... * (1+pn+pn^2+pn^3+...pn^kn)

(3)   同余模公式:

(a+b)%m=(a%m+b%m)%m

(a*b)%m=(a%m*b%m)%m

 

1: 对A进行素因子分解

分解A的方法:

A首先对第一个素数2不断取模,A%2==0时 ,记录2出现的次数+1,A/=2;

当A%2!=0时,则A对下一个连续素数3不断取模...

以此类推,直到A==1为止。

 

注意特殊判定,当A本身就是素数时,无法分解,它自己就是其本身的素数分解式。

 

最后得到A = p1^k1 * p2^k2 * p3^k3 *...* pn^kn.
      故 A^B = p1^(k1*B) * p2^(k2*B) *...* pn^(kn*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)].


3: 用递归二分求等比数列1+pi+pi^2+pi^3+...+pi^n:

(1)若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))

(2)若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)根据整数的唯一分解定理可以将一个数分成质因数的幂相乘的形式,记录下来质因数和出现的次数。

2)根据约数和公式来求和,同时用此公式,还要解觉等比公式的和的问题。利用二分递归来进行运算。同时又在二分递归的时候进行快速幂。



#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
#include<cmath>
#include<queue>
#define mod 9901
#define LL long long
using namespace std;
LL primeper[2500000],cnt[2500000];
LL pow1(LL a,LL b)
{
    LL r=1,ba=a;
    while(b!=0)
    {
        if(b&1)
            r=(r*ba)%mod;
        ba=(ba*ba)%mod;
        b>>=1;
    }
    return r;
}
LL so(LL x,LL y)
{
    if(!y)
    return 1;
    else if(y&1)
        return ( (1+pow1(x,y/2+1))*so(x,y/2) )%mod;//注意1+pow1(x,y/2+1)是看作一个整体,要用()扩起来再*so()
    else
        return ( (1+pow1(x,y/2+1))*so(x,y/2-1)+pow1(x,y/2) )%mod;
}
int main()
{
    LL i,j,k;LL n,m;
    while(scanf("%lld%lld",&n,&m)!=EOF)
    {
        memset(cnt,0,sizeof(cnt));
        for(k=0,i=2;i*i<=n;i++)
        {
            if(n%i==0)
            {
                primeper[k]=i;
                while(n%i==0)
                {
                    cnt[k]++;
                    n/=i;
                }
                k++;
            }
        }
        if(n!=1)
        {
            primeper[k]=n;
            cnt[k]++;
            k++;
        }
        LL ans=1;
        for(i=0;i<k;i++)
            ans=(ans*(so(primeper[i],cnt[i]*m))%mod )%mod;
        printf("%lld\n",ans);
    }
    return 0;
}
奋斗

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值