1153 -- p次方求和

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值