poj 1845 Sumdiv(同余模公式)
2016-06-01 11:36
375 查看
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).
题意:求A^B,由于A和B很大,所以这里用到同余模公式
思路:根据唯一分解定理A = p1^a1*p2^a2.....*pn^an,
则A^B=(p1^a1*p2^a2.....*pn^an)^B = p1^(a1*B)*p2^(a2*B).....*pn^(an*B)
所有正约数和S = (1+p1+p1^2+...p1^(a1*B)) + (1+p2+p2^2+...p2^(a2*B)) + ... (1+pn+pn^2+...pn^(an*B))
可见ss = (1+p+p^2+...p^n)是等比数列的和,递归二分求该等比数列的和
当n为奇数时,一共有偶数项
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))
当n为偶数时,一共有奇数项
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
= {1, 1}, prim
, cnt = 0, t, b;
struct node
{
LL p, a;
} stu
;
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;
}
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).
题意:求A^B,由于A和B很大,所以这里用到同余模公式
思路:根据唯一分解定理A = p1^a1*p2^a2.....*pn^an,
则A^B=(p1^a1*p2^a2.....*pn^an)^B = p1^(a1*B)*p2^(a2*B).....*pn^(an*B)
所有正约数和S = (1+p1+p1^2+...p1^(a1*B)) + (1+p2+p2^2+...p2^(a2*B)) + ... (1+pn+pn^2+...pn^(an*B))
可见ss = (1+p+p^2+...p^n)是等比数列的和,递归二分求该等比数列的和
当n为奇数时,一共有偶数项
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))
当n为偶数时,一共有奇数项
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
= {1, 1}, prim
, cnt = 0, t, b;
struct node
{
LL p, a;
} stu
;
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;
}
相关文章推荐
- C#开发:openfiledialog的使用
- Moq -.NET的Mocking库
- C# 设计模式系列教程-装饰模式
- HTML学习笔记(3) - HTML标签
- 工具链编译选项
- 压力测试工具siege的用法
- 引入外部字体文件进行设置
- IO端口与IO内存详解
- Java网络编程——基于UDP的Socket编程
- android模拟购物app输入支付密码
- 【Leetcode】之Climbing Stairs
- Android 平板中 自定义键盘(popuwindow) 居于屏幕左下方 仿微信的密码输入界面
- [置顶] XListView上拉刷新下拉加载(网络请求json)
- bind9报 bad owner name (check-names)错误的解决方法
- OpenCV HOG+SVM行人检测:从训练到检测
- jquery插件
- 电子购物网站导航制作
- Light oj-1100 - Again Array Queries,又是这个题,上次那个题用的线段树,这题差点就陷坑里了,简单的抽屉原理加暴力就可以了,真是坑~~
- 产品打包工具的制作,ant,编译源码,打jar包,打tag,打war包,备份release版本等
- mysql索引操作