Sumdiv 等比数列求和

                              Sumdiv

 

Sumdiv
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 15364   Accepted: 3790

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

 
 

大致题意:

求A^B的所有约数(即因子)之和,并对其取模 9901再输出。

 

解题思路:

要求有较强 数学思维 的题

应用定理主要有三个:

要求有较强 数学思维 的题

应用定理主要有三个:

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

上式红色加粗的前半部分恰好就是原式的一半,那么只需要不断递归二分求和就可以了,后半部分为幂次式,将在下面第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);

   上式红色加粗的前半部分恰好就是原式的一半,依然递归求解

 

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <list>
13 #include <iomanip>
14 #include <cstdlib>
15 #include <sstream>
16 using namespace std;
17 typedef long long LL;
18 const int INF=0x4fffffff;
19 const double EXP=1e-6;
20 const int MS=1000005;
21 const LL mod=9901;
22 
23 LL pow_mod(LL x,LL n)
24 {
25       LL res=1;
26       while(n)
27       {
28             if(n&(1LL))
29                   //res=res*x%mod;
30                   res=((res%mod)*(x%mod))%mod;
31             n>>=1;
32            // x=x*x%mod;
33            x=(x%mod)*(x%mod);
34       }
35       return res;
36 }
37 
38 LL calc(LL x,LL y)       //   sigma (0->y)  x^i
39 {
40       if(y==0)
41             return 1;
42       if(y&(1LL))
43             return ((calc(x,y/2)%mod)*((pow_mod(x,y/2+1)+1)%mod))%mod;
44             //return calc(x,y/2)*(1+pow_mod(x,y/2+1));
45       else
46             return (    (  (calc(x,y/2-1)%mod)* (  (pow_mod(x,y/2+1)+1)%mod)  )   %mod+pow_mod(x,y/2)  )%mod;
47            //return calc(x,y/2-1)*(pow_mod(x,y/2+1)+1)+pow_mod(x,y/2);
48 }
49 
50 LL prime[MS];
51 LL cnt[MS];
52 
53 int main()
54 {
55       LL x,y;
56       cin>>x>>y;
57       LL t=(LL)(sqrt(x*1.0)+EXP);
58       LL k=0;
59       for(LL i=2;i<=t;i++)
60       {
61             if(x%i==0)
62             {
63                   prime[k]=i;
64                   while(x%i==0)
65                   {
66                         cnt[k]++;
67                         x/=i;
68                   }
69                   k++;
70             }
71       }
72       if(x!=1LL)
73       {
74             prime[k]=x;
75             cnt[k++]=1;
76       }
77 
78       LL ans=1LL;
79       for(LL i=0;i<k;i++)
80             ans=(ans%mod*calc(prime[i],cnt[i]*y)%mod)%mod;
81       cout<<ans<<endl;
82       return 0;
83 }

 

posted @ 2015-04-16 21:34  daydaycode  阅读(229)  评论(0编辑  收藏  举报