# poj 1845 Sumdiv（同余模公式）

http://poj.org/problem?id=1845

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

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

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

(a+b)%mod = (a%mod+b%mod)%mod;

(a*b)%mod = (a%mod*b%mod)%mod;

#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <stack>
#include <vector>
#include <map>

using namespace std;

#define N 1002000
#define INF 0xfffffff
#define PI acos (-1.0)
#define EPS 1e-8
#define met(a, b) memset (a, b, sizeof (a))

typedef long long LL;

LL a;
int isprim[N] = {1, 1}, prim[N], cnt = 0, t, b;

struct node
{
LL p, a;
} stu[N];

void Init ()
{///素数打表
for (int i=2; i<1001000; i++)
{
if (!isprim[i])
{
prim[cnt++] = i;

for (int j=i+i; j<1001000; j+=i)
isprim[j] = 1;
}
}
}

void Fenjie ()
{///唯一分解定理
t = 0;

for (int i=0; i<cnt; i++)
{
int k = 0;
if (a%prim[i]==0)
{
while (a%prim[i]==0)
{
k++;
a /= prim[i];
}

stu[t].a = k;
stu[t++].p = prim[i]%9901;
}
if (a==1) break;
}

if (a!=1)
stu[t].p = a%9901, stu[t++].a = 1;
}

LL Quick_Pow (int m, int n)
{///快速幂
LL temp = 1;
while (n)
{
if (n&1)
temp = temp * m % 9901;
n >>= 1;
m = m * m % 9901;
}
return temp;
}

LL sum (int p, LL n)
{///等比数列递归二分求和
if (n==0) return 1;

if (n%2) return (sum (p, n/2) * (1+Quick_Pow(p, n/2+1)))%9901;
else return (sum (p, n/2-1) * (1+Quick_Pow(p, n/2+1)) + Quick_Pow(p, n/2))%9901;
}

int main ()
{
Init();

while (scanf ("%I64d %d", &a, &b) != EOF)
{
met (stu, 0);

if (a==1 || !a)
{
puts ("1");
continue;
}

Fenjie();
LL ans = 1;

for (int i=0; i<t; i++)
ans = (ans * sum(stu[i].p, stu[i].a*b)%9901) % 9901;

printf ("%I64d\n", ans%9901);
}
return 0;
}


#### POJ1845-Sumdiv

2011-07-31 16:29:30

#### POJ 1845：Sumdiv 快速幂+逆元

2015-09-17 10:41:56

#### poj 1845 Sumdiv 数论--等比数列和(逆元或者递归)

2013-10-02 12:10:14

#### poj1845--Sumdiv(数论篇3--真滴是数论啊。。。。)

2015-01-23 08:50:14

#### POJ 1845 Sumdiv（数论）

2015-07-23 10:19:45

#### POJ 1845 Sumdiv (快速幂+质因数+约数和公式+同余模)

2015-08-17 20:51:01

#### poj1845（唯一分解定理，等比数列求和，约数个数公式）

2016-10-13 21:06:58

#### poj-1845-Sumdiv-数论-快速幂取模+快速分解因式

2013-02-19 14:46:25

#### POJ 1845-Sumdiv 数论 +快速幂&&筛素&&分解质因数&&求因数之和的模板

2015-11-20 23:35:55

#### POJ 1845 Sumdiv (快速幂)

2014-01-19 20:30:11