• POJ


    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). 
     
    题意:输入A,B  求A^B的所有因子之和
     
    思路: 首先求一个数的所有因子之和,我们就可以使用我们的唯一分解定理
    (1+p1^1+p1^2+p1^3...p1^n)*(1+p2^1.....p2^n)*...*(1+pn^1+....+pn^n)
    然后每一个括号里面我们可以用一个等比数列公式来求得 
    a1*(1-q^n)/(1-q)    
    不过既然我们要用除法,那我们为了保证精确度肯定要求逆元,还有求q^n的时候,因为范围比较大,你就要使用快速幂求得
    这也说了,这是一个数的所有因子之和,
    题目所求得是   A^B的所有因子之和,所以你要想想,因为数据比较大所以我们不能直接求解,这样取模容易丢失精度
    运用算术基本原理  4^6=(2^2) ^6=2^12
    所以可以直接算出素因子时直接个数乘以幂数即可
     
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #define mod 9901
    #define MAX 1001
    using namespace std;
    typedef long long ll;
    int cnt;
    vector<ll> prime;
    vector<ll> times;
    ll qpow(ll a,ll b)//快速幂加快求幂数
    {
        ll ans=1;
        while(b)
        {
            if(b&1) ans=(ans*a)%mod;
            b>>=1;
            a=(a*a)%mod;
        }
        return ans;
    }
    void divide(ll n) {//求出n里所有的素数银子
        for(ll i=2;i*i<=n;++i) {
            if(n%i==0) {
                prime.push_back(i);ll cnt=0;
                while(n%i==0) {n/=i;++cnt;}
                times.push_back(cnt);
            }
        }
        if(n>1) {prime.push_back(n);times.push_back(1);}
    }
    int main()
    {
        ll a,b;
        scanf("%lld%lld",&a,&b);
        divide(a);
        ll ans=1;
        for(int i=0,end=prime.size();i<end;++i) {
            times[i]*=b;//乘了括号外的乘方
            if((prime[i]-1)%mod==0) ans=ans*(times[i]+1)%mod;//当底数为1时,就是乘以项数
            else ans=ans*((qpow(prime[i],times[i]+1)-1+mod)*qpow(prime[i]-1,mod-2)%mod)%mod;//等比数列公式
        }
        printf("%lld
    ",ans);
        return 0;
    }
  • 相关阅读:
    十万个为什么 —— 理化篇
    地行、地貌
    工业镜头基础知识整理
    parfor —— matlab 下的并行循环
    浅谈设计模式在GIS中的应用
    android ellipsize 属性详解
    Servlet 学习总结-1
    搜索引擎有用的外链建设的方式
    Servlet 学习总结-2
    如何用批处理命令批量配对重命名
  • 原文地址:https://www.cnblogs.com/Lis-/p/9693860.html
Copyright © 2020-2023  润新知