p次方求和
Time Limit:1000MS Memory Limit:65536K
Total Submit:200 Accepted:47
Description
一个很简单的问题,求1^p+2^p+3^p+……+n^p的和。
Input
第一行单独一个数字t表示测试数据组数。接下来会有t行数字,每行包括两个数字n,p,
输入保证0< n< =1000,0< =p< =1000。
Output
输出1^p+2^p+3^p+……+n^p对10003取余的结果,每个结果单独占一行。
Sample Input
2
10 1
10 2
Sample Output
55
385
Source
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AK1153 {
class Program {
static int erfen(int n, int p) {
int a = n;
int b = p;
int result = 1;
while (b != 0) {
if (b % 2 == 1) {
result = (result * a) % 10003; //乘一个a,也就是i
}
a = (a * a) % 10003; //平方
b /= 2; //把这个数截一半
}
return result;
}
static void Main(string[] args) {
int t = int.Parse(Console.ReadLine());
while (t-- > 0) {
string[] sb = Console.ReadLine().Split();
int n = int.Parse(sb[0]), p = int.Parse(sb[1]);
int sum = 0;
for (int i = 1; i <= n; i++)
sum = (sum + erfen(i, p)) % 10003;
Console.WriteLine(sum);
}
}
}
}