POJ 1845-Sumdiv(厉害了这个题)
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).
题意很简单a的b次方的因数和对9901取模;
这个题题意很简单,但是看数据量加操作肯定超时,涵盖知识量极多。
1.快速幂:递归求解指数运算。
指数过大无法运算,虽然不知道他们是怎么做的,估计跟这个差不多。
PS
typedef long long LL;
LL power(int a,int b){
LL ans=1;
while(b){
if(p&1) ans=ans*a; /判断是否为奇数(可以%2,按位或&可能快一点)
a=a*a;
b>>=1;
}
return ans;
以2 ^ 8 和2 ^ 9为例,
a=2,b=8;
b是偶数,进行下一步
a=22=4;
b=4;
b是偶数,进行下一步
a=44=16:
b=2;
next
a=1616=256
b=1;
next
b是奇数;
ans=1a=256;
只用了log2n次的乘法;
2.A^B的所有约数之和为:
sum = [1+p1+p12+…+p1 (a1*B)] * [1+p2+p22+…+p2(a2*B)] … [1+pn+pn2+…+pn(an*B)].
3.埃氏筛法
直接板子(这里我用了近似的思想,当时还没明白这个方法)
int a[maxx];
int b[maxx+1];
int gg(int n)
{
int p=0;//记录素数个数
for(int i=0;i<n+1;i++)b[i]=1;
b[0]=0;
b[1]=0;
//准备完毕
for(int i=2;i<=n;i++){
if(b[i]){
a[p++]=i;//记录素数和个数
for(int j=2*i;j<=n;j+=i)b[j]=0;//剔除倍数
}
}
return p;//返回素数个数
}
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#define MOD 9901
using namespace std;
int a,b,z;
int p[10000]; //质数
int p_n[10000];//质数个数
void Zhi()//质数打表,求小于根号a的质数
{
int t = a;
for (int i=2; i*i<=a; i++)//开方运算比乘法用时间长
{
if (t%i==0)
{
p[z]=i;
p_n[z]=1;
t/=i;
while (t%i==0)
{
p_n[z]++;
t/=i;
}
z++;
}
if (t==1) break;
if (i!=2)
i++;//2.3.5.7.9...
}
if (t!=1)//本身就是质数
{
p[z]=t;
p_n[z]=1;
z++;
}
}
int Mi(int a, int b)//快速幂
{
int res = 1;
a%=MOD;
while(b)
{
if (b&1) res = (res * a)%MOD;
a=(a*a)%MOD;
b>>=1;
}
return res;
}
int Ys(int p , int n)//递归求等比系数
{
if (n==0) return 1;
if (n%2==1)
return ((Mi(p,n/2+1)+1) * Ys(p,n/2))%MOD;
else
return ((1+Mi(p,n/2+1)) * Ys(p,n/2-1) + Mi(p,n/2))%MOD;
}
int main()
{
while (scanf("%d%d",&a,&b)!=EOF)
{
z=0;//质数个数
Zhi();
int ans = 1;
for (int i=0; i<z; i++)
{
ans=(ans*Ys(p[i],p_n[i]*b))%MOD;
}
printf("%d\n",ans);
}
return 0;
}
POJ 1845-Sumdiv(厉害了这个题)的更多相关文章
- poj 1845 POJ 1845 Sumdiv 数学模板
筛选法+求一个整数的分解+快速模幂运算+递归求计算1+p+p^2+````+p^nPOJ 1845 Sumdiv求A^B的所有约数之和%9901 */#include<stdio.h>#i ...
- poj 1845 Sumdiv 约数和定理
Sumdiv 题目连接: http://poj.org/problem?id=1845 Description Consider two natural numbers A and B. Let S ...
- POJ 1845 Sumdiv 【二分 || 逆元】
任意门:http://poj.org/problem?id=1845. Sumdiv Time Limit: 1000MS Memory Limit: 30000K Total Submissions ...
- poj 1845 Sumdiv (等比求和+逆元)
题目链接:http://poj.org/problem?id=1845 题目大意:给出两个自然数a,b,求a^b的所有自然数因子的和模上9901 (0 <= a,b <= 50000000 ...
- POJ 1845 Sumdiv#质因数分解+二分
题目链接:http://poj.org/problem?id=1845 关于质因数分解,模板见:http://www.cnblogs.com/atmacmer/p/5285810.html 二分法思想 ...
- POJ 1845 Sumdiv [素数分解 快速幂取模 二分求和等比数列]
传送门:http://poj.org/problem?id=1845 大致题意: 求A^B的所有约数(即因子)之和,并对其取模 9901再输出. 解题基础: 1) 整数的唯一分解定理: 任意正整数都有 ...
- POJ 1845 Sumdiv
快速幂+等比数列求和.... Sumdiv Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12599 Accepted: 305 ...
- POJ 1845 Sumdiv(逆元)
题目链接:Sumdiv 题意:给定两个自然数A,B,定义S为A^B所有的自然因子的和,求出S mod 9901的值. 题解:了解下以下知识点 1.整数的唯一分解定理 任意正整数都有且只有唯一的方式 ...
- POJ 1845 Sumdiv (整数唯一分解定理)
题目链接 Sumdiv Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25841 Accepted: 6382 Desc ...
随机推荐
- 一天学一个Linux命令:第一天 ls
文章更新于:2020-03-02 注:本文参照 man ls 手册,并给出使用样例. 文章目录 一.命令之`ls` 1.名字及介绍 2.语法格式 3.输出内容示例 4.参数 二.命令实践 1.`ls ...
- 30.5 Map遍历方法
package day30_2_Map; import java.util.HashMap; import java.util.Map; import java.util.Set; /* 方法一.用e ...
- git如何清除远程 __pycahce__ 文件
第一步,清除已经存在的缓存文件 >> git rm -r -f --cached */__pycache__ rm 'common/__pycache__/__init__.cpython ...
- list 的sublist 隐藏 bug
list A = new list(); list a = A.sublist(0,3); 假如对a进行增加或者删除 会 同样改变A里的值,即其实a仅仅是A的一个试图,而不是一个新的list 对象,所 ...
- 深入理解JS原型与原型链
函数的prototype 1.函数的prototype属性 *每个函数都有一个prototype属性,它默认指向一个Object空对象(即称为原型对 象) * 原型对象中都有一个属性construct ...
- spring源码阅读笔记08:bean加载之创建bean
上文从整体视角分析了bean创建的流程,分析了Spring在bean创建之前所做的一些准备工作,并且简单分析了一下bean创建的过程,接下来就要详细分析bean创建的各个流程了,这是一个比较复杂的过程 ...
- work of 1/6/2016
part 组员 今日工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云 UI动态布局改进和攻克疑难 6 继续下滑条等增删补减 ...
- [javascript] jquery的父子兄弟节点查找
jQuery.parent(expr) 找父亲节点,可以传入expr进行过滤,比如$("span").parent()或者$("span").parent(&q ...
- python调用word2vec工具包安装和使用指南
python调用word2vec工具包安装和使用指南 word2vec python-toolkit installation and use tutorial 本文选译自英文版,代码注释均摘自本文, ...
- 基于nodejs的游戏服务器
开源一个四年前自己写的node服务器,有兴趣的可以继续开发-- 架构为mysql,redis,node. 数据格式为 protocol buff 如果只做简单的演示,这个架构非常适合你.. 还是typ ...