Sumdiv
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 15745 | Accepted: 3894 |
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).
The natural divisors of 8 are: 1,2,4,8. Their sum is 15.
15 modulo 9901 is 15 (that should be output).
Source
最近在学习数论的一些知识点,感觉无处下手,只好按照相应的题目进行学习,做这个题需要用到以下知识点。
(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
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
const int h = 10001;
int n,m;
struct node
{
int x;
int y;
}q[h];
long long int power(long long int pn,long long int pm) ///反复平方法来求A^B 省时间
{
long long int sq = 1;
while(pm>0)
{
if(pm%2)
{
sq = (sq*pn)%9901;
}
pm = pm / 2;
pn = pn * pn % 9901;
}
return sq;
}
long long int updata(long long int pn,long long int pm) ///递归二分求等比数列的和
{
if(pm == 0)
{
return 1;
}
if(pm%2)
{
return (updata(pn,pm/2)*(1+power(pn,pm/2+1)))%9901; /// 当pm为奇数时,有公式来求等比数列的和 (1 + p + p^2 +...+ p^(n/2)) * (1 + p^(n/2+1))
}
else
{
return (updata(pn,pm/2-1)*(1+power(pn,pm/2+1)) + power(pn,pm/2))%9901; ///当pm为偶数时,有公式来求等比数列的和 (1 + p + p^2 +...+ p^(n/2-1)) * (1+p^(n/2+1)) + p^(n/2);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int k = 0;
for(int i=2;i*i<=n;) ///寻找质因子,一个很好的方法
{
if(n%i == 0)
{
q[k].x = i;
q[k].y = 0;
while(n%i == 0)
{
q[k].y++;
n /= i;
}
k++;
}
if(i == 2)
{
i++;
}
else
{
i = i + 2;
}
}
if(n!=1)
{
q[k].x = n;
q[k].y = 1;
k++;
}
int ans = 1;
for(int i=0;i<k;i++)
{
ans = (ans*(updata(q[i].x,q[i].y*m)%9901)%9901);
}
printf("%d\n",ans);
}
return 0;
}