题目链接:https://ac.nowcoder.com/acm/contest/9981/A
分析
经过推演,我们可以得出:
- n = 2, ans = 1
- n = 3, ans = 26 * 1 + 25 * 2
- n = 4, ans = 26 * 26 * 1 + 25 * 26 * 2 + 25 * 25 * 3
- n = 5, ans = 26 * 26 * 26 * 1 + 25 * 26 * 26 * 2 + 25 * 25 * 26 * 3 + 25 * 25 * 25 * 4
- …
所以我们可以预处理 26 的各个次方的前缀和来算出答案
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
ll n,pre[1000007];
ll qpow(ll x,ll y)
{
ll res = 1;
while(y)
{
if(y & 1) res = res * x % mod;
x = x * x % mod;
y >>= 1;
}
return res;
}
int main()
{
pre[0] = 1;
for(ll i=1;i<=1000000;i++) pre[i] = (pre[i - 1] + qpow(26, i)) % mod;
cin>>n;
ll ans = 0;
for(ll i=1;i<=n-1;i++) ans = (ans + qpow(25, i - 1) * pre[n - i - 1] % mod * i % mod) % mod;
cout<<ans;
return 0;
}