题目:Self powers
The series, 11 + 22 + 33 + ... + 1010 = 10405071317.
Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000.
这个题意很明确,解决起来比较简单。
数学描述
代码实现
//https://projecteuler.net/problem=48
//Self powers
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <cmath>
#include <BaseTsd.h>
using namespace std;
//N:序列最大值
//M:保留的最末数字个数
INT64 SelfPower(int N, int M)
{
INT64 sum = 0;
INT64 base = (INT64)pow((double)10, int(M));
for (int i = 1; i <= N; ++i)
{
INT64 product = 1;
for (int j = 1; j <= i; ++j)
{
product *= i;
product %= base;
}
sum += product;
sum %= base;
}
return sum;
}
int _tmain(int argc, _TCHAR* argv[])
{
int N = 1000, M = 10;
INT64 ans = SelfPower(N, M);
cout<<"SelfPower("<<N<<","<<M<<") = "<<ans<<endl;
system("pause") ;
return 0;
}
输入:N=1000,M=10
输出:SelfPower(1000,10) = 9110846700