Sumdiv 等比数列求和
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 15364 | Accepted: 3790 |
Description
Input
Output
Sample Input
2 3
Sample Output
15
Hint
The natural divisors of 8 are: 1,2,4,8. Their sum is 15.
15 modulo 9901 is 15 (that should be output).
Source
大致题意:
求A^B的所有约数(即因子)之和,并对其取模 9901再输出。
解题思路:
要求有较强 数学思维 的题
应用定理主要有三个:
要求有较强 数学思维 的题
应用定理主要有三个:
(1) 整数的唯一分解定理:
任意正整数都有且只有一种方式写出其素因子的乘积表达式。
A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^kn) 其中pi均为素数
(2) 约数和公式:
对于已经分解的整数A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^kn)
有A的所有因子之和为
S = (1+p1+p1^2+p1^3+...p1^k1) * (1+p2+p2^2+p2^3+….p2^k2) * (1+p3+ p3^3+…+ p3^k3) * .... * (1+pn+pn^2+pn^3+...pn^kn)
(3) 同余模公式:
(a+b)%m=(a%m+b%m)%m
(a*b)%m=(a%m*b%m)%m
有了上面的数学基础,那么本题解法就很简单了:
1: 对A进行素因子分解
分解A的方法:
A首先对第一个素数2不断取模,A%2==0时 ,记录2出现的次数+1,A/=2;
当A%2!=0时,则A对下一个连续素数3不断取模...
以此类推,直到A==1为止。
注意特殊判定,当A本身就是素数时,无法分解,它自己就是其本身的素数分解式。
最后得到A = p1^k1 * p2^k2 * p3^k3 *...* pn^kn.
故 A^B = p1^(k1*B) * p2^(k2*B) *...* pn^(kn*B);
2:A^B的所有约数之和为:
sum = [1+p1+p1^2+...+p1^(a1*B)] * [1+p2+p2^2+...+p2^(a2*B)] *...* [1+pn+pn^2+...+pn^(an*B)].
3: 用递归二分求等比数列1+pi+pi^2+pi^3+...+pi^n:
(1)若n为奇数,一共有偶数项,则:
1 + p + p^2 + p^3 +...+ p^n
= (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))
上式红色加粗的前半部分恰好就是原式的一半,那么只需要不断递归二分求和就可以了,后半部分为幂次式,将在下面第4点讲述计算方法。
(2)若n为偶数,一共有奇数项,则:
1 + p + p^2 + p^3 +...+ p^n
= (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);
上式红色加粗的前半部分恰好就是原式的一半,依然递归求解
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
typedef long long LL;
const int INF=0x4fffffff;
const double EXP=1e-;
const int MS=;
const LL mod=; LL pow_mod(LL x,LL n)
{
LL res=;
while(n)
{
if(n&(1LL))
//res=res*x%mod;
res=((res%mod)*(x%mod))%mod;
n>>=;
// x=x*x%mod;
x=(x%mod)*(x%mod);
}
return res;
} LL calc(LL x,LL y) // sigma (0->y) x^i
{
if(y==)
return ;
if(y&(1LL))
return ((calc(x,y/)%mod)*((pow_mod(x,y/+)+)%mod))%mod;
//return calc(x,y/2)*(1+pow_mod(x,y/2+1));
else
return ( ( (calc(x,y/-)%mod)* ( (pow_mod(x,y/+)+)%mod) ) %mod+pow_mod(x,y/) )%mod;
//return calc(x,y/2-1)*(pow_mod(x,y/2+1)+1)+pow_mod(x,y/2);
} LL prime[MS];
LL cnt[MS]; int main()
{
LL x,y;
cin>>x>>y;
LL t=(LL)(sqrt(x*1.0)+EXP);
LL k=;
for(LL i=;i<=t;i++)
{
if(x%i==)
{
prime[k]=i;
while(x%i==)
{
cnt[k]++;
x/=i;
}
k++;
}
}
if(x!=1LL)
{
prime[k]=x;
cnt[k++]=;
} LL ans=1LL;
for(LL i=;i<k;i++)
ans=(ans%mod*calc(prime[i],cnt[i]*y)%mod)%mod;
cout<<ans<<endl;
return ;
}
Sumdiv 等比数列求和的更多相关文章
- POJ 1845 (约数和+二分等比数列求和)
题目链接: http://poj.org/problem?id=1845 题目大意:A^B的所有约数和,mod 9901. 解题思路: ①整数唯一分解定理: 一个整数A一定能被分成:A=(P1^K1) ...
- hoj3152-Dice 等比数列求和取模
http://acm.hit.edu.cn/hoj/problem/view?id=3152 Dice My Tags (Edit) Source : Time limit : sec Memory ...
- luogu1397 [NOI2013]矩阵游戏 (等比数列求和)
一个比较显然的等比数列求和,但有一点问题就是n和m巨大.. 考虑到他们是在幂次上出现,所以可以模上P-1(费马小定理) 但是a或c等于1的时候,不能用等比数列求和公式,这时候就要乘n和m,又要变成模P ...
- Codeforces 963A Alternating Sum(等比数列求和+逆元+快速幂)
题目链接:http://codeforces.com/problemset/problem/963/A 题目大意:就是给了你n,a,b和一段长度为k的只有'+'和‘-’字符串,保证n+1被k整除,让你 ...
- bzoj 4555 [Tjoi2016&Heoi2016]求和 NTT 第二类斯特林数 等比数列求和优化
[Tjoi2016&Heoi2016]求和 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 679 Solved: 534[Submit][S ...
- ZOJ-3774 Power of Fibonacci——等比数列求和&&等价替换
题目 求 $\displaystyle \sum_{i=1}^n F_i^k$,($1 \leq n\leq 10^{18},1 \leq k\leq 10^5$),答案对 $10^9+9$ 取模. ...
- 2019河北省大学生程序设计竞赛(重现赛)B 题 -Icebound and Sequence ( 等比数列求和的快速幂取模)
题目链接:https://ac.nowcoder.com/acm/contest/903/B 题意: 给你 q,n,p,求 q1+q2+...+qn 的和 模 p. 思路:一开始不会做,后面查了下发现 ...
- [zoj 3774]Power of Fibonacci 数论(二次剩余 拓展欧几里得 等比数列求和)
Power of Fibonacci Time Limit: 5 Seconds Memory Limit: 65536 KB In mathematics, Fibonacci numbe ...
- CodeForces Round #191 (327C) - Magic Five 等比数列求和的快速幂取模
很久以前做过此类问题..就因为太久了..这题想了很久想不出..卡在推出等比的求和公式,有除法运算,无法快速幂取模... 看到了 http://blog.csdn.net/yangshuolll/art ...
随机推荐
- Awk中调用shell命令
Awk中调用shell命令 需求 在awk中,有时候需要调用linux系统中命令,如计算字符串的MD5值,并保存下来. 方法参考 call a shell command from inside aw ...
- HDU 1002 分类: ACM 2015-06-18 23:03 9人阅读 评论(0) 收藏
昨天做的那题其实和大整数相加类似.记得当初我大一寒假就卡在这1002题上,结果之后就再也没写题... 到今天终于把大整数相加写了一遍. 不过写的很繁琐,抽时间改进一下写简洁一点. #include&l ...
- UVALive 7455 Linear Ecosystem (高斯消元)
Linear Ecosystem 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/B Description http://7xj ...
- 用LinkedHashMap实现LRU算法
(在学习操作系统时,要做一份有关LRU和clock算法的实验报告,很多同学都应该是通过数组去实现LRU,可能是对堆栈的使用和链表的使用不是很熟悉吧,在网上查资料时看到了LinkedHashMap,于是 ...
- STM32 常用GPIO操作函数记录
STM32读具体GPIOx的某一位是1还是0 /** * @brief Reads the specified input port pin. * @param GPIOx: where x can ...
- 使用WITH AS 的ROW_NUMBER分页
WITH tempTable AS( --复杂查询语句) SELECT * FROM (select ROW_NUMBER() Over( order by xxx) as rowNum, ...
- Jquery 获取文件内容
$('.ke-edit-iframe').contents().find('body').text() <iframe class="ke-edit-iframe" hide ...
- C#中的Collection 3
IList<T> 和 ICollection<T> 最重要的一些类型 List<T>: Encapsulates[T], like array, but also ...
- [转] [Visual Studio 2012] 找回 建立單元測試 選單
原文链接:http://www.dotblogs.com.tw/yc421206/archive/2013/03/08/95920.aspx Step1.建立選單 在VS2012選單,Tools→Cu ...
- Function.caller
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/caller 非标准 ...