zoukankan      html  css  js  c++  java
  • 数学:A^B的约数(因子)之和对MOD取模

    POJ1845

    首先把A写成唯一分解定理的形式

    分解时让A对所有质数从小到大取模就好了

    然后就有:A = p1^k1 * p2^k2 * p3^k3 *...* pn^kn

    然后有: A^B = p1^(k1*B) * p2^(k2*B) *...* pn^(kn*B);

    约数和公式:

    对于已经分解的整数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)

    那么A^B就可以是

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

    求等比数列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))
    
    上式红色加粗的前半部分恰好就是原式的一半,那么只需要不断递归二分求和就可以了,后半部分为幂次式,将在下面第4点讲述计算方法。
    
    (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);
    
       上式红色加粗的前半部分恰好就是原式的一半,依然递归求解

    p^n直接快速幂就可以了

     1 #include<cstdio>
     2 #include<cstring>
     3 const int mod=9901;
     4 const int maxn=10005;
     5 int A,B;
     6 int fatcnt;
     7 int prime[maxn];
     8 long long factor[10][2];
     9 void get_prime()
    10 {
    11     memset(prime,0,sizeof(prime));
    12     for(int i=2;i<=maxn;i++)
    13     {
    14         if(!prime[i]) prime[++prime[0]]=i;
    15         for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++)
    16         {
    17             prime[prime[j]*i]=1;
    18             if(i%prime[j]==0) break;
    19         }
    20     }
    21 }
    22 int get_factors(long long x)
    23 {
    24     fatcnt=0;
    25     long long tmp=x;
    26     for(int i=1;prime[i]<=tmp/prime[i];i++)
    27     {
    28         factor[fatcnt][1]=0;
    29         if(tmp%prime[i]==0)
    30         {
    31             factor[fatcnt][0]=prime[i];
    32             while(tmp%prime[i]==0)
    33             {
    34                 factor[fatcnt][1]++;
    35                 tmp/=prime[i];
    36             }
    37             fatcnt++;
    38         }
    39     }
    40     if(tmp!=1)
    41     {
    42         factor[fatcnt][0]=tmp;
    43         factor[fatcnt++][1]=1;
    44     }
    45     return fatcnt;
    46 }
    47 long long pow_mod(long long a,long long n)
    48 {
    49     long long res=1;
    50     long long tmp=a%mod;
    51     while(n)
    52     {
    53         if(n&1)
    54         {
    55             res*=tmp;
    56             res%=mod;
    57         }
    58         n>>=1;
    59         tmp*=tmp;
    60         tmp%=mod;
    61     }
    62     return res;
    63 }
    64 long long sum(long long p,long long n)
    65 {
    66     //1+p+p^2+````+p^n
    67     if(p==0) return 0;
    68     if(n==0) return 1;
    69     if(n&1)
    70         return ((1+pow_mod(p,n/2+1))%mod*sum(p,n/2)%mod)%mod;
    71     else
    72         return ((1+pow_mod(p,n/2+1))%mod*sum(p,n/2-1)+pow_mod(p,n/2)%mod)%mod;
    73 }
    74 int main()
    75 {
    76     get_prime();
    77     while(scanf("%d%d",&A,&B)==2)
    78     {
    79         get_factors(A);
    80         long long ans=1;
    81         for(int i=0;i<fatcnt;i++)
    82         {
    83             ans*=(sum(factor[i][0],B*factor[i][1])%mod);
    84             ans%=mod;
    85         }
    86         printf("%I64d
    ",ans);
    87     }
    88     return 0;
    89 }
  • 相关阅读:
    Evidence on the impact of sustained exposure to air pollution on life expectancy
    International Immunopharmacology/ Perinatal and early childhood environmental factors influencing allergic asthma immunopathogenesis
    Projections of the effects of climate change on allergic asthma: the contribution of aerobiology
    Word List 2
    GRE : TC21
    Grammar Mistake: What is a faulty tense sequence?
    Biology 02: Respiration
    127 super()方法详解
    128 类的多态和多态性
    126 菱形继承问题
  • 原文地址:https://www.cnblogs.com/aininot260/p/9574789.html
Copyright © 2011-2022 走看看