2021牛客寒假算法基础集训营1 A.串(组合)

题目链接:https://ac.nowcoder.com/acm/contest/9981/A

分析

经过推演,我们可以得出:

  1. n = 2, ans = 1
  2. n = 3, ans = 26 * 1 + 25 * 2
  3. n = 4, ans = 26 * 26 * 1 + 25 * 26 * 2 + 25 * 25 * 3
  4. 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值