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--周边知识点
一般Redis服务器内存超过128G内存的机器是非常少的 很少在redis中缓存视频,除非是快播,一般都是缓存文本字段 redis可视化的工具和SQL的管理工具是不一样的,最好是使用REDIS的she ...
- Spring MVC-表单(Form)标签-文本框(Text Box)示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_textbox.htm 说明:示例基于Spring MVC 4.1.6. 以下示例 ...
- study reference
CVPR2018 ReID论文简评 2017CVPR ICCV和NIPS在Person Reidentification方向的相关工作小结 CVPR 2018 Person Re-ID相关论文 pre ...
- 【cl】eclipse基本设置(字体、配置JDK)
字体 找到上面的菜单“windows”打开Preferences 在弹出的设置窗口中找到“colors and fonts” 将 ...
- 热修复JSPatch之实战教程
接上篇<热修复JSPatch之接口设计>,在这篇文章主要给大家讲述一下怎样高速具备热修复能力,当然了假设有人有志于把JSPatch系统的学习,甚至用JSPatch进行开发的.就没有必要 ...
- udev的使用-minicom没有权限打开串口,更改 ttyUSB0 的权限
udev的使用-minicom没有权限打开串口,更改 ttyUSB0 的权限 使用minicom打开串口会提示没有权限,必需要用 sudo,怎样更改串口设备的权限能够让普通用户读写呢? 事实上仅仅要更 ...
- cocos2d-x 是怎样渲染的
要知道 是怎样渲染的:要先选中 就可以 谁知道: c ocos2d-x 是怎样渲染的 ? 每一个CCNODE自己有draw 北京-菜菜: :: draw draw负重渲染 ************** ...
- HDU Distinct Values
/* 一开始想到的是 对于每个区间操作 先按左端点排序(包含的区间 留这打的区间) 我们维护pos表示 a数组找到了哪 对于当前这个区间 只需要找 pos--r这个区间 用set维护能用的数 没放到a ...
- HDU 4386 Quadrilateral(数学啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4386 Problem Description One day the little Jack is p ...
- hdoj--1258--Sum It Up(dfs)
Sum It Up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...