题意:

项的自幂级数求和为 11 + 22 + 33 + … + 1010 = 10405071317。

求如下一千项的自幂级数求和的最后10位数字:11 + 22 + 33 + … + 10001000

思路:

  1. 求最后十位数字 % 1010 即可。

  2. 对于快速幂中数据溢出的问题,有两种解决方法:

    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. 方法二:对于快速幂中的乘法,我们可以写个与快速幂类似的快速乘法,在快速乘法的过程中不断取模来保持数据在范围之内。

  3. 对于 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( 大数求余 )的更多相关文章

  1. project euler 48 Self powers 解决乘法爆long long

    题目链接 求 $ 1^1+2^2+\cdots + 1000^{1000} $ %1e10 的结果. 唯一的坑点是会爆longlong, 所以用特殊的乘法. #include <iostream ...

  2. POJ 2635 The Embarrassed Cryptographer(大数求余)

    题意:给出一个大数,这个大数由两个素数相乘得到,让我们判断是否其中一个素数比L要小,如果两个都小,输出较小的那个. 分析:大数求余的方法:针对题目中的样例,143 11,我们可以这样算,1 % 11 ...

  3. POJ2635-The Embarrassed Cryptographer 大数求余

    题目链接:http://poj.org/problem?id=2635 题目分析: http://blog.csdn.net/lyy289065406/article/details/6648530

  4. (大数 求余) 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 ...

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

  6. Large Division (大数求余)

    Given two integers, a and b, you should check whether a is divisible by b or not. We know that an in ...

  7. 大数求模 sicily 1020

        Search

  8. 2016中国大学生程序设计竞赛 - 网络选拔赛 1001 A water problem (大数取余)

    Problem Descripton Two planets named Haha and Xixi in the universe and they were created with the un ...

  9. 如何运用同余定理求余数【hdoj 1212 Big Number【大数求余数】】

    Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. HDU 2295

    二分答案+重复覆盖.注意返回的条件哦,不能光套模板. #include <iostream> #include <cstdio> #include <cstring> ...

  2. 搭建单机CDH环境,并更新spark环境

    搭建单机CDH环境,并更新spark环境 1,安装VMWare Player,http://dlsw.baidu.com/sw-search-sp/soft/90/13927/VMware_playe ...

  3. cocos2d-x的gitignore配置

    # Ignore thumbnails created by windows Thumbs.db # Ignore files build by Visual Studio *.obj *.exe * ...

  4. VMware workstation虚拟机不能联网解决方法

    以备后用. 第一步:先设置VMware的编辑——虚拟网络编辑器,启用VMnet8,NAT模式,如下图所示. 其实就是VMware默认的设置,无须更改,如果不小心改了,点击还原默认设置. 第二步:虚拟机 ...

  5. Android 四大组件学习之ContentProvider二

    上节学习了什么是ContentProvider.以及ContentProvider的作用.以及什么是URL.本节就对上节学习的知识做一个实践,也就是定义自己的ContentProvider 好.实践是 ...

  6. 火狐Vimperator插件

    http://www.iplaysoft.com/vimium-and-vimperator.html http://wangbixi.com/x2923/comment-page-1/

  7. Linux下用ImageMagick玩图像魔术【转】

    本文转载自:http://www.linuxdiyf.com/linux/11680.html 不管你知不知道,现在是一个用ImageMagick的好机会,至少,如果你是一个Linux用户的话.这是一 ...

  8. Working with macro signatures

    https://docs.kentico.com/k11/macro-expressions/troubleshooting-macros/working-with-macro-signatures ...

  9. gdb的使用(转)

    gdb使用 转自清华大学操作系统实验指导书 gdb 是功能强大的调试程序,可完成如下的调试任务: 设置断点 监视程序变量的值 程序的单步(step in/step over)执行 显示/修改变量的值 ...

  10. typescript 基本数据类型

    1.boolen 布尔类型 let boolen1: boolen = false; 2.number 数字类型 let num1: number = 0b110;//二进制 let num2: nu ...