1153 -- p次方求和

来源:互联网 发布:泽诺尼亚s下载最新数据 编辑:程序博客网 时间:2024/05/18 00:02

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

210 110 2

Sample Output

55385

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);                }            }        }    }


0 0
原创粉丝点击