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

题目链接

求 $ 11+22+\cdots + 1000^{1000} $ %1e10 的结果。

唯一的坑点是会爆longlong, 所以用特殊的乘法。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const ll mod = 1e10;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
ll mul(ll a, ll b) {
ll ret = 0;
while(b) {
if(b&1) {
ret += a;
if(ret >= mod)
ret -= mod;
}
a += a;
if(a >= mod)
a -= mod;
b >>= 1;
}
return ret;
}
ll pow(ll a, ll b) {
ll ret = 1;
while(b) {
if(b&1) {
ret = mul(ret, a)%mod;
}
a = mul(a, a)%mod;
b >>= 1;
}
return ret;
}
int main()
{
ll sum = 0;
for(ll i = 1; i <= 1000; i++) {
sum = (sum + pow(i, i))%mod;
}
cout<<sum<<endl;
return 0;
}
上一篇:mysql触发器new和old


下一篇:Linux C 文件操作,系统调用 -- open()、read() 和 标准I/O库 -- fopen()、fread()