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 ...
随机推荐
- Springboot2(二)通过微信熟悉熟悉Spring-boot yml配置文件
前言:Spring-boot的yml配置文件,这里就不在借助人.狗介绍了,试试套下微信! 创建yml文件 值得注意的是下图中有三种命名方法,前两种是对的,且第二种必须是横线而不是下划线! yml文件的 ...
- bootstraptable 必备知识点
1.如何动态刷新表中数据? (1).无参刷新: $("#table").bootstrapTable('refresh'); (2).带参刷新: var opt = { url: ...
- NullPointerException的处理新方式,Java14真的太香了
在Java语言中,处理空指针往往是一件很头疼的事情,一不小心,说不定就搞出个线上Bug,让你的绩效考核拿到3.25.最近新出的Java14,相信大家都有所耳闻,那么今天就来看看,面对NullPoint ...
- 深入理解new运算符
在 JavaScript 中,new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例.创建一个对象很简单,为什么我们还要多此一举使用 new 运算符呢?它到底有什么样的魔力? 认 ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(二)之Introduction to Objects
The genesis of the computer revolution was a machine. The genesis of out programming languages thus ...
- 分屏神器PoweToys
win+~调用设置分屏界面,shift+软件拖到分屏位置
- Xshell 设置右键粘贴即是复制
打开工具->选项->键盘和鼠标面板 1.鼠标部分的右击设置"粘贴剪切板的内容". 2.选择部分,在"自动将所选文本复制到剪切板"前打勾
- 如果这篇文章说不清epoll的本质,那就过来掐死我吧!
转载自:https://www.toutiao.com/i6683264188661367309/ 目录 一.从网卡接收数据说起 二.如何知道接收了数据? 三.进程阻塞为什么不占用cpu资源? 四.内 ...
- 详解 Properties类
(请观看本人博文--<详解 I/O流>) Properties类: 概念: Properties 类的对象 是 一个持久的属性集 Properties 可 保存在流中 或 从流中加载 属性 ...
- 3. string
let str = "my string"; 1. str.startsWith('my'); //true2.str.endsWith('my'); //false3.str.i ...