# poj 1845 Sumdiv（同余模公式）

169人阅读 评论(0)

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;
}


1
0

个人资料
• 访问：79897次
• 积分：3561
• 等级：
• 排名：第10994名
• 原创：277篇
• 转载：6篇
• 译文：0篇
• 评论：17条
阅读排行
最新评论