[TYVJ-1119] 快速求冪

分治算法。

/*
 * TYVJ-1119
 * mike-w
 * 2012-9-29
 */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

const __int64 mod=1012;

__int64 foo(__int64 a, __int64 b)
{
	if(b==0)
		return 1;
	else
	{
		int t=foo(a, b/2);
		return (b&0x1)?(t*t*a%mod):(t*t%mod);
	}
}

int main(void)
{
	__int64 a, b;
	int n;
	scanf("%d", &n);
	while(n-->0)
	{
		scanf("%I64d%I64d", &a, &b);
		printf("%I64d\n", foo(a, b));
	}

	return 0;
}


發佈了204 篇原創文章 · 獲贊 4 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章