首页 > 其他 > 详细

nyoj 420 p次方求和 【快速幂】

时间:2014-09-13 00:49:44      阅读:290      评论:0      收藏:0      [点我收藏+]

题意。。。

策略:rt

代码:

#include <stdio.h>
#include <string.h>
#define temp 10003
int ans(int n, int p){
	int res = 1;
	n %= temp;
	while(p){
		if(p&1) res = (n*res)%temp;
		n = (n*n)%temp;
		p /= 2;
	}
	return res;
}
int main(){
	int t, n, p;
	scanf("%d", &t);
	while(t --){
		scanf("%d%d", &n, &p);
		int sum = 0; //这里的sum初始化必须为0
		for(int i = 1; i <= n; i ++){ //i从1开始
			sum = (sum+ans(i, p))%temp;
		}
		printf("%d\n", sum);
	}
	return 0;
} 

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=420

nyoj 420 p次方求和 【快速幂】

原文:http://blog.csdn.net/shengweisong/article/details/39237961

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!