题意:

项的自幂级数求和为 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 4505

    哈哈哈哈哈哈哈哈哈,省赛,一等奖,一定的一定的一定的一定的... #include <iostream> #include <cstdio> #include <cstr ...

  2. 【bzoj1001】【狼抓兔子】

    1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MB Submit: 12719 Solved: 3017 [Submit][ ...

  3. HDU2191_悼念512汶川大地震遇难同胞——珍惜如今,感恩生活(背包/多重背包)

    解题报告 题目传送门 题意: 中文不多说; 思路: 基础多重背包,每一个物品有多个能够选.转换成01背包解. #include <iostream> #include <cstrin ...

  4. Java IO之简单输入输出

    Java中的IO分为两个部分,以InputStream和Reader为基类的输入类,以OutputStream和Writer为基类的输出类. 当中InputStream和OutputStream以字节 ...

  5. 【MongoDB】windows平台搭建Mongo数据库复制集(相似集群)(三)

    关于windows平台搭建Mongo数据库复制集这个话题,我已经在前面写了两篇博客 第一篇: 怎样在windows平台搭建Mongo数据库复制集 第二篇: 数据同步和故障自适应測试 在本篇里面,咱们重 ...

  6. 【SQLSERVER】MD5注意事项

    sql中使用MD5加密是很常见的事情,但是不知道注意点的人还是会即便是拷贝网络上的写法也是会出现错误的. 举个例子简单说明: 由上图我们可以发现相同的字符串但是得到的MD5加密的字符却是不相同的,那么 ...

  7. Item 8:析构函数不要抛出异常 Effective C++笔记

    Item 8: Prevent exceptions from leaving destructors. 析构函数不要抛出异常 因为析构函数经常被自己主动调用,在析构函数中抛出的异常往往会难以捕获,引 ...

  8. luogu2508 [HAOI2008]圆上的整点

    题目大意 给出\(r\),求圆\(x^2+y^2=r^2\)上坐标均为整数的点数.\(n<=2,000,000,000\) 总体思路 我们看到这个数据大小,还是个数学题,想到这个的时间复杂度应当 ...

  9. xargs命令【转】

    本文转载自:http://man.linuxde.net/xargs

  10. Cache操作类

    封装类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...