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 ...
随机推荐
- elasticsearch6.4中文文档
男朋友翻译的,我这边做一个查看入口,分享给大家,O(∩_∩)O哈哈~ 版权声明:本文为博主原创文章,无需授权即可转载,甚至无需保留以上版权声明,转载时请务必注明作者. https://blog.csd ...
- 0929MySQL JOIN的算法
http://www.cnblogs.com/starhu/p/6418842.html http://www.cnblogs.com/starhu/p/6418833.html http://www ...
- 【ACM】poj_2210_Metric Time_201308011933
Metric TimeTime Limit: 1000MS Memory Limit: 65536K Total Submissions: 2550 Accepted: 783 Descripti ...
- jdbc 读取oracle long raw 字段,里面存的是文本
jdbc 读取oracle long raw 字段,里面存的是文本 参考: http://singlewolf.iteye.com/blog/278769 http://blog.csdn.net/r ...
- Netty In Action中文版 - 第十五章:选择正确的线程模型
http://blog.csdn.net/abc_key/article/details/38419469 本章介绍 线程模型(thread-model) 事件循环(EventLoop) 并发(Con ...
- 去掉文本框前后的空格(JS+JQuery)
表单验证时,需要去除文本框前后的空格才可以正确通过验证.以前看到过一句话:任何设计和代码都要对用户足够宽容. <input type="text" class="p ...
- 项目产品化——Excel数据库模板化导入(java)
Excel导入可能是代码开发中比較常见的功能,一个项目假设有多个地方须要excel导入数据库.那么开发的工作量也将比較大,项目产品化过程中,将这么一个类似的功能进行封装合并也是必要的.封装好的代码仅仅 ...
- POJ3185 The Water Bowls 反转(开关)
Description The cows have a line of 20 water bowls from which they drink. The bowls can be either ri ...
- c++11 实现半同步半异步线程池
感受: 随着深入学习,现代c++给我带来越来越多的惊喜- c++真的变强大了. 半同步半异步线程池: 事实上非常好理解.分为三层 同步层:通过IO复用或者其它多线程多进程等不断的将待处理事件加入到队列 ...
- Don't Block on Async Code
http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html This is a problem that is brough ...