zzuli 2180 GJJ的日常之沉迷数学(逆元)
Contest - 河南省多校连萌(四)
**
Problem E: GJJ的日常之沉迷数学
**
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 307 Solved: 35
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 1
6 10
Sample Output
Case 1: 3
Case 2: 72559411
题中给是求一个等比数列的前n项和取模(1e9+7);
s=(q^n-1)/(q-1)
现在求前n+1项,逆元下就好,q==1的时候特判
( a / b ) % p =a * inv ( b , p ) %p =( a%p * inv ( b , p )%p ) %p
直接公式就好了
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 1000000007;
LL q_mod(LL a, LL b)
{
LL ans = 1ll;
while(b)
{
if(b&1) ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
int main()
{
int n, k, cnt = 0;
LL a, b, x;
while(~scanf("%d%d", &k, &n))
{
if(k == 1)
{
printf("Case %d: %d\n", ++cnt, n+1);
continue;
}
a = q_mod(k, n+1) - 1ll;
b = k - 1ll;
x = q_mod(b, mod-2ll);
printf("Case %d: %lld\n", ++cnt, a*x%mod);
}
return 0;
}