Project Euler 48 Self powers( 大数求余 )
题意:
项的自幂级数求和为 11 + 22 + 33 + … + 1010 = 10405071317。
求如下一千项的自幂级数求和的最后10位数字:11 + 22 + 33 + … + 10001000。
思路:
求最后十位数字 % 1010 即可。
对于快速幂中数据溢出的问题,有两种解决方法:
1. 方法一:对于两个数 x y,现在想求 x * y % MOD,可以将 x 表示成 a * DIGS + b,y 表示成 c * DIGS + d,x * y % MOD 则等价与 ( a * c * DIGS2 + a * d * DIGS + b * c * DIGS + b * d ) % MOD ( DIGS = 1E5 ) 这样进行分解后就可以有效的避免数据溢出。
2. 方法二:对于快速幂中的乘法,我们可以写个与快速幂类似的快速乘法,在快速乘法的过程中不断取模来保持数据在范围之内。对于 i % 10 == 0 的情况 i i % MOD 一定为 0
方法一代码:
/*************************************************************************
> File Name: euler048t2.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年07月01日 星期六 16时41分22秒
************************************************************************/
#include <stdio.h>
#include <inttypes.h>
#define MOD 10000000000
#define MAX_N 1000
#define DIGS 100000
int64_t Multi(int64_t x , int64_t y) {
int64_t a , b , c , d , ans = 0;
a = x / DIGS; b = x % DIGS;
c = y / DIGS; d = y % DIGS;
ans = (ans + ((a * d) % MOD * DIGS) % MOD) % MOD;
ans = (ans + ((b * c) % MOD * DIGS) % MOD) % MOD;
ans = (ans + (b * d) % MOD) % MOD;
return ans;
}
int64_t quick_pow(int64_t a , int64_t b , int64_t mod) {
int64_t ret = 1;
while (b) {
if (b & 1) ret = Multi(ret , a);
a = Multi(a , a);
b >>= 1;
}
return ret;
}
int32_t main() {
int64_t sum = 0;
for (int32_t i = 1 ; i <= MAX_N ; i++) {
if (i % 10 == 0) continue;
sum = (sum + quick_pow(i , i , MOD)) % MOD;
}
printf("%"PRId64"\n",sum);
return 0;
}
方法二代码:
/*************************************************************************
> File Name: euler048.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年07月01日 星期六 16时21分58秒
************************************************************************/
#include <stdio.h>
#include <inttypes.h>
#define MAX_N 1000
#define MOD 10000000000
int64_t quick_multi(int64_t a , int64_t b , int64_t mod) {
int64_t ret = 0;
while (b) {
if (b & 1) ret = (ret + a) % mod;
a = (a << 1) % mod;
b >>= 1;
}
return ret % mod;
}
int64_t quick_power(int64_t a , int64_t b , int64_t mod) {
int64_t ret = 1;
while (b) {
if (b & 1) ret = quick_multi(ret , a , mod);
a = quick_multi(a , a , mod);
b >>= 1;
}
return ret % mod;
}
int32_t main() {
int64_t sum = 0;
for (int32_t i = 1 ; i <= MAX_N ;i++) {
if (i % 10 == 0) continue;
sum = (sum + (quick_power((int64_t)i , (int64_t)i , MOD))) % MOD;
}
printf("%"PRId64"\n",sum);
return 0;
}
Project Euler 48 Self powers( 大数求余 )的更多相关文章
- project euler 48 Self powers 解决乘法爆long long
题目链接 求 $ 1^1+2^2+\cdots + 1000^{1000} $ %1e10 的结果. 唯一的坑点是会爆longlong, 所以用特殊的乘法. #include <iostream ...
- POJ 2635 The Embarrassed Cryptographer(大数求余)
题意:给出一个大数,这个大数由两个素数相乘得到,让我们判断是否其中一个素数比L要小,如果两个都小,输出较小的那个. 分析:大数求余的方法:针对题目中的样例,143 11,我们可以这样算,1 % 11 ...
- POJ2635-The Embarrassed Cryptographer 大数求余
题目链接:http://poj.org/problem?id=2635 题目分析: http://blog.csdn.net/lyy289065406/article/details/6648530
- (大数 求余) Large Division Light OJ 1214
Large Division Given two integers, a and b, you should check whether a is divisible by b or not. We ...
- Project Euler 29 Distinct powers( 大整数质因数分解做法 + 普通做法 )
题意: 考虑所有满足2 ≤ a ≤ 5和2 ≤ b ≤ 5的整数组合生成的幂ab: 22=4, 23=8, 24=16, 25=3232=9, 33=27, 34=81, 35=24342=16, 4 ...
- Large Division (大数求余)
Given two integers, a and b, you should check whether a is divisible by b or not. We know that an in ...
- 大数求模 sicily 1020
Search
- 2016中国大学生程序设计竞赛 - 网络选拔赛 1001 A water problem (大数取余)
Problem Descripton Two planets named Haha and Xixi in the universe and they were created with the un ...
- 如何运用同余定理求余数【hdoj 1212 Big Number【大数求余数】】
Big Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
随机推荐
- Redis Hash 的 HSET、HGET、HMSET、HMGET 性能测试
[压测环境] 操作系统: Ubuntu 14.04 LTS Linux版本: 3.13.0-24-generic x86_64 GNU/Linux 处理器: 4核的 AMD Athlon(tm) II ...
- 神奇的JAVA多态
以前理解了基本思想,这版本的演示和应用比较真实. 顺路下来抽象方法和类,接口,就顺理成章啦... JAVA文件放一块了,分别对照前一个帖子的文件名: ///////////////////////// ...
- COGS——C1176. [郑州101中学] 月考
http://cogs.pro/cogs/problem/problem.php?pid=1176 [题目描述] 在上次的月考中Bugall同学违反了考场纪律还吃了处分,更可气的是在第二天的校会时 间 ...
- 在java中,怎样跳出当前的多重循环?
</pre>直接用break ;详细举比例如以下:<p></p><p></p><p></p><pre name ...
- 开源项目 apk cfg and android app path profiling
暑假里面完毕的一个小项目,limitation还是挺多的. 期待未来有更大的motivation 去完好它.通过此次的项目设计,对于smali的语法更加了解了,对于进一步学习android app的安 ...
- C++中sort()及qsort() (不完整介绍)
在平时刷算法题和oj的时候,排序算法是最经常用到的算法之一:且在各类算法书的目录中 也通常是将各种排序算法放在最前面来讲,可见排序算法的重要性.可能许多人都在算法书中有学过冒泡.快速排序的方法,也都大 ...
- Mysql 索引需要了解的几个注意
索引是做什么的? 索引用于快速找出在某个列中有一特定值的行.不使用索引,MySQL必须从第1条记录开始然后读完整个表直到找出相关的行.表越大,花费的时间越多.如果表中查询的列有一个索引,MySQL能快 ...
- HDU 1754 I Hate it (线段树最大值模板)
思路:与我发表的上一遍求和的思想一样 仅仅是如今变成求最大值而已 AC代码: #include<iostream> #include<cstdio> #include< ...
- element-UI中table表格的@row-click事件和@selection-change耦合了
<el-table ref="multipleTable" :data="tableData" tooltip-effect="dark&quo ...
- angular4 select 绑定(ngModel)对象
欢迎加入前端交流群交流知识&&获取视频资料:749539640 <h1>My Application</h1> <select [(ngModel)]=& ...