PE48

/* The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317. Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000. */ #include <iostream> using namespace std; void problem48(int n) { int* result = new int[11]; for (int i = 10; i>=0; i--) result[i] = 0; for (int a=1; a<=n; a++) { int temp = a; int* digit = new int[11]; int* add = new int[11]; for (int i = 10; i>=0; i--) { digit[i] = temp % 10; add[i] = digit[i]; temp /= 10; } for (int t=1; t<a; t++) { for (int t=1; t<a; t++) { for (int i = 10; i>=0; i--) { digit[i] += add[i]; if (digit[i] > 9 && i!=0) { digit[i] = digit[i] % 10; digit[i-1]++; } if (digit[0] > 9) { digit[0] = digit[0] % 10; } } } for (int i = 0; i<11; i++) add[i] = digit[i]; } delete [] add; for (int i = 10; i>=0; i--) { result[i] += digit[i]; if (result[i] > 9 && i!=0) { result[i] = result[i] % 10; result[i-1]++; } if (result[0] > 9) { result[0] = digit[0] % 10; } } delete [] digit; } for (int i = 1; i<11; i++) cout << result[i]; delete [] result; } int main() { problem48(10); cout << endl; problem48(1000); } 

 

#include <iostream> int main() { unsigned long long sol = 0; for (unsigned u = 1; u <= 1000; ++u) { unsigned long long tmp = u; for (unsigned v = 1; v < u; ++v) (tmp *= u) %= 10000000000ULL; sol += tmp; } std::cout << sol % 10000000000ULL << '/n'; } 

你可能感兴趣的:(delete)