Sumdiv
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 16666 | Accepted: 4164 |
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
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define LL long long
#define mod 9901
#define N 10005
int A, B;
int mul_pow(int a, int k)
{
int res = 1;
while(k)
{
if(k & 1) res = (res * a) % mod;
a = ((LL)a * a) % mod;
k >>= 1;
}
return res;
}
set<int> S;
void doit()
{
S.clear();
int t = A;
for(int i = 2; i <= t; i++)
while(t != i)
{
if(t % i == 0)
{
S.insert(i);
t /= i;
}
else break;
}
S.insert(t);
}
int sum(int a, int p)
{
if(a == 0) return 1;
if(p == 0) return 1;
if(p & 1) return (1 + mul_pow(a, p / 2 + 1)) * sum(a, p / 2) % mod;
else return ((1 + mul_pow(a, p / 2 + 1)) * sum(a, p / 2 - 1) + mul_pow(a, p / 2)) % mod;
}
int main()
{
while(~scanf("%d%d", &A, &B))
{
if(A == 1 || A == 0)
{
printf("1\n");
continue;
}
doit();
int ans = 1;
for(set<int>::iterator it = S.begin(); it != S.end(); it++)
{
int num = 0;
while(A % (*it) == 0)
{
num++;
A /= (*it);
}
ans = (ans * sum(*it, num * B)) % mod;
}
printf("%d\n", ans);
}
return 0;
}
/*
50000000 50000000
0 0
1 0
2 0
2 1
2 2
3 0
3 1
3 2
3 3
4 5
5 8
*/