博客
关于我
等比数列求和 (快速幂 + 逆元)
阅读量:394 次
发布时间:2019-03-05

本文共 3078 字,大约阅读时间需要 10 分钟。

     求一个等比数例之和, 并让他对一个数取模。

     用到等比数列求和公式, 快速幂, 逆元。

     不会证明, 下面给出代码。

#include 
#include
#include
typedef long long ll;ll multi(ll a,ll b,ll m) // 二分乘法{ ll ans = 0; a %= m; while(b) { if(b & 1) { ans = (ans + a) % m; b--; } b >>= 1; a = (a + a) % m; } return ans;}ll quick_mod(ll a,ll b,ll mod){ ll res = 1; while(b) { if(b % 2 == 1) res = multi(res, a, mod); // 二分乘法 不然会数据溢出 b >>= 1; a = multi(a, a, mod); // 二分乘法 不然会数据溢出 } return res;}int main(){ ll a, b, mod=9901; scanf("%I64d %I64d",&a, &b); // 首项为 1 , 公比为 a,长度 为 b 的等比数例求和 printf("%I64d\n",((quick_mod(a,b,mod*(a-1)) - 1) / (a-1))); return 0;}

Sumdiv

 

 

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 23623   Accepted: 5876

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). 

 

思路: 举一个例子: 36, 1。  

          先把 36 分解成质因数, 2, 2,3, 3。

            ans = 1 ,  2,   2*2,   1*(3 +  3*3),   2 * (3 + 3*3)  ,  2*2 *(3 * 3). 之和。

#include 
#include
#include
typedef long long ll;int p[10005];void prime() // 打一个素数表{ p[1] = 2; int cnt = 1; for(int i=3; i<=10000; i+=2) { int flag = true; for(int j=2; j*j<=i; j++) { if(i%j == 0) { flag = false; break; } } if(flag) p[++cnt] = i; }}ll multi(ll a,ll b,ll m) // 二分乘法{ ll ans = 0; a %= m; while(b) { if(b & 1) { ans = (ans + a) % m; b--; } b >>= 1; a = (a + a) % m; } return ans;}ll quick_mod(ll a,ll b,ll mod) // 快速幂{ ll res = 1; while(b) { if(b % 2 == 1) res = multi(res, a, mod); b >>= 1; a = multi(a, a, mod); } return res;}int main(){ ll a, b, mod=9901; scanf("%I64d %I64d",&a, &b); if(a == 1) { printf("1\n"); return 0; } prime(); ll ans = 1; int num = 0; for(int i=1; p[i]*p[i]<=a; i++) { if(a % p[i] == 0) // 寻找每个质因数 和 它的数量 num。 { num=0; while(a % p[i] == 0) { num++; a /= p[i]; } ll m =(p[i] - 1) * mod; ans *= ((quick_mod(p[i], num*b+1, m) + m - 1) / (p[i]-1)); // 等比数列求和 // printf("%lld\n",(quick_mod(p[i], num*b+1, m) + m - 1) / (p[i]-1) % mod); ans %= mod; } } ll m = mod * (a - 1); if(a > 1) printf("%I64d\n",ans * ((quick_mod(a, b+1, m) + m - 1) / (a-1)) % mod); else printf("%I64d\n",ans % mod); return 0;}

 

转载地址:http://edkzz.baihongyu.com/

你可能感兴趣的文章
Mysql中文乱码问题完美解决方案
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
Mysql中的 IFNULL 函数的详解
查看>>
mysql中的collate关键字是什么意思?
查看>>
MySql中的concat()相关函数
查看>>
mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
查看>>
MySQL中的count函数
查看>>
MySQL中的DB、DBMS、SQL
查看>>
MySQL中的DECIMAL类型:MYSQL_TYPE_DECIMAL与MYSQL_TYPE_NEWDECIMAL详解
查看>>
MySQL中的GROUP_CONCAT()函数详解与实战应用
查看>>
MySQL中的IO问题分析与优化
查看>>
MySQL中的ON DUPLICATE KEY UPDATE详解与应用
查看>>
mysql中的rbs,SharePoint RBS:即使启用了RBS,内容数据库也在不断增长
查看>>
mysql中的undo log、redo log 、binlog大致概要
查看>>
Mysql中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
查看>>
mysql中的字段如何选择合适的数据类型呢?
查看>>
MySQL中的字符集陷阱:为何避免使用UTF-8
查看>>
mysql中的数据导入与导出
查看>>