Codeforces#533 C.Ayoub and Lost Array(组合数学&递推)

问题

Ayoub had an array a of integers of size n and this array had two interesting properties:

  1. All the integers in the array were between l and r (inclusive).
  2. The sum of all the elements was divisible by 3.

Unfortunately, Ayoub has lost his array, but he remembers the size of the array n and the numbers l and r, so he asked you to find the number of ways to restore the array.

Since the answer could be very large, print it modulo 109+7(i.e. the remainder when dividing by 109+7). In case there are no satisfying arrays (Ayoub has a wrong memory), print 0.

输入

The first and only line contains three integers n, l and r (1≤n≤2⋅1e5,1≤l≤r≤1e9) — the size of the lost array and the range of numbers in the array.

输出
Print the remainder when dividing by 109+7 the number of ways to restore the array.

样例
input
2 1 3
output
3
input
3 2 2
output
1
input
9 9 99
output
711426616
注释
In the first example, the possible arrays are : [1,2],[2,1],[3,3].
In the second example, the only possible array is [2,2,2].

分析
题意是在l~r的区间内找任意k个数,这些数相加正好是3的倍数,问有几种这样的k个数。就是有多少种这样的组合。
因为是求和是3的倍数,这里先推一下和是3的倍数的数都是有哪些数组成。当k=1时,只有l~~r内余数为0的数组成,假如这种数有num0个;即dp[0][1] = num0;当k=2时,可以用两个在l~~r余数为0的数进行组合,即dp[0][1]*num0种;也可以用一个在l~~r余数为1的数(假如这种数有num1个,即dp[1][1] = num1)和一个在l~~r余数为2的数(假如这种数有num2个,即dp[2][1] = num2)进行组合,即dp[1][1] *num2种;也可以用一个在l~~r余数为2的数和一个在l~~r余数为1的数进行组合,即dp[2][1]*num1种,所以dp[0][2] = dp[0][1] *num0+dp[1][1]*num2+dp[2][1]*num1种。同理可以证出来dp[1][2],dp[2][2];以此类推
dp[0][i] = dp[0][i-1]*num0+dp[1][i-1]*num2+dp[2][i-1]*num1;
dp[1][i] = dp[0][i-1]*num1+dp[1][i-1]*num0+dp[2][i-1]*num2;
dp[2][i] = dp[0][i-1]*num2+dp[0][i-1]*num2+dp[1][i-1]*num1;
最终dp[0][k]即是任意k个数组成的和是3的倍数的种类数。

#include<iostream>
using namespace std;
const long long mod = 1e9 + 7;
long long dp[3][200200];
int main()
{
	long long n, l, r;
	cin >> n >> l >> r;
	for (int i = 0; i < 3; i++)
	{
		dp[i][0] = 0;
		dp[i][1] = r / 3;
		dp[i][1] -= (l - 1) / 3;
	}
	switch (r % 3)
	{
	case 1: dp[1][1]++; break;
	case 2: dp[2][1]++; dp[1][1]++; break;
	default:break;
	}
	switch ((l - 1) % 3)
	{
	case 1: dp[1][1]--; break;
	case 2: dp[2][1]--; dp[1][1]--; break;
	default:break;
	}
	//以上为求num0,num1,num2。下面为公式递推。
	for (int i = 2; i <= n; i++)
	{
		dp[0][i] = ((dp[1][i - 1] * dp[2][1]) % mod + (dp[0][i - 1] * dp[0][1]) % mod + (dp[2][i - 1] * dp[1][1]) % mod) % mod;
		dp[1][i] = ((dp[0][i - 1] * dp[1][1]) % mod + (dp[2][i - 1] * dp[2][1]) % mod + (dp[1][i - 1] * dp[0][1]) % mod) % mod;
		dp[2][i] = ((dp[0][i - 1] * dp[2][1]) % mod + (dp[1][i - 1] * dp[1][1]) % mod + (dp[2][i - 1] * dp[0][1]) % mod) % mod;
	}
	cout << dp[0][n] << endl;
	return 0;
}
发布了27 篇原创文章 · 获赞 13 · 访问量 1708

猜你喜欢

转载自blog.csdn.net/weixin_43855330/article/details/86586101