2180: GJJ的日常之沉迷数学
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 340 Solved: 46
Submit Status Web Board
Description
GJJ每天都要膜拜一发数学大佬,因为GJJ的数学太差了。这不,GJJ又遇到难题了,他想求助WJJ,但是WJJ这几天忙于追妹子,哪有时间给他讲题, 于是GJJ求助于热爱ACM的你,Acmer们能帮帮他吗?问题是求: k^0 + k^1 +...+ k^(n) mod p (0 < k < 100, 0 <= n <= 10^9, p = 1000000007)
例如:6^0 + 6^1 +...+ 6^(10) mod 1000000007 (其中k = 6, n = 10, p = 1000000007)
Input
输入测试数据有多组,每组输入两个整数k, n
Output
每组测试数据输出:Case #: 计算结果
Sample Input
2 16 10
Sample Output
Case 1: 3Case 2: 72559411
题解:对等比数列的前n和取模,s=(q^n-1)/(q-1),当q=1时特判。
代码:
#include<cstdio>
typedef long long LL;
const LL MOD =1e9 +7;
LL quickMod(LL a,LL b)
{
LL ans=1;
while(b)
{
if(b&1)
ans=ans*a%MOD;
a=a*a%MOD;
b>>=1;
}
return ans;
}
int main()
{
int cnt=0;
int n,m;LL a,b,x;
while(~scanf("%d%d",&n,&m))
{
if(n==1)
{
printf("Case %d: %d\n", ++cnt, m+1);
continue;
} //s=(q^n-1)/(q-1),
a=quickMod(n,m+1)-1; //(q^n-1)
b=n-1; //(q-1)
x=quickMod(b,MOD-2); //1/(q-1)
printf("Case %d: %lld\n", ++cnt, a*x%MOD);
}
return 0;
}