链接:https://www.nowcoder.com/acm/contest/136/J
来源:牛客网
第一行有3个正整数n,k,p。
输出一行,一个正整数,表示按照要求铺满n个格子需要多少洋灰三角,由于输出数据过大,你只需要输出答案模1000000007(1e9+7)后的结果即可。
对于100%的测试数据:
1 ≤ n ≤ 1000000000
1 ≤ k,p ≤ 1000
分析:
k=1时:
f(n)为等差数列,S(n)=n*(n-1)/2*p+n
k!=1时:
f(n) = k*f(n-1)+p
S(n)=f(1)+f(2)+...+f(n)=1+k+k^2+...+k^(n-1)+k^(n-2)*p+2*k^(n-3)*p+...+(n-2)*k*p+(n-1)*p
=(k^n-1)/(k-1)+p*(k^n-1)/(k-1)^2-p*n/(k-1)=(1+p/(k-1))*(k^n-1)/(k-1)-p*n/(k-1)
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e6+10;
const ll mod = 1e9+7;
const double pi = acos(-1.0);
const double eps = 1e-8;
ll qow( ll a, ll b ) {
ll ans = 1;
while( b ) {
if( b&1 ) {
ans = ans*a%mod;
}
a = a*a%mod;
b /= 2;
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
ll n, k, p;
while( cin >> n >> k >> p ) {
if( k == 1 ) {
cout << (n-1)*n/2*p+n << endl;
} else {
ll ans=(1+p*qow(k-1,mod-2)%mod)%mod*((qow(k,n)-1+mod)%mod)%mod*qow(k-1,mod-2)%mod;
ans=(ans-p*qow(k-1,mod-2)%mod*n%mod+mod)%mod;
cout << ans << endl;
}
}
return 0;
}
原文:https://www.cnblogs.com/l609929321/p/9500814.html