time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Ayoub had an array aa of integers of size nn and this array had two interesting properties:
- All the integers in the array were between l and r(inclusive).
- 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 10^9+7 (i.e. the remainder when dividing by 10^9+7 ). In case there are no satisfying arrays (Ayoub has a wrong memory), print 0 .
Input
The first and only line contains three integers nn , ll and rr (1≤n≤2⋅105^,1≤l≤r≤10^91≤n≤2⋅10^5,1≤l≤r≤10^9 ) — the size of the lost array and the range of numbers in the array.
Output
Print the remainder when dividing by 109+7109+7 the number of ways to restore the array.
Examples
Input
Copy
2 1 3
Output
Copy
3
Input
Copy
3 2 2
Output
Copy
1
Input
Copy
9 9 99
Output
Copy
711426616
Note
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]
题意:
有一个长度为n的数组,数组里面的数都在[l,r]中,问你有这个数组有多少种(数组是有顺序的)
解析:
这道题...把我心态搞炸了....
一开始看读错题意,以为[l,r]里面的每一个数只能用一次,比赛的时候怎么想都想不出来,但是后来发现样例[2,2,2]...
然后按照这个思路写了......一直调样例,过不去...又发现数组是有顺序的.....[1,2],[2,1]
后来想到了dp直接做就可以了dp[i][j],表示长度为i,里面元素和%3==j的数组的个数
然后根据这个来写递推就可以了
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 2e5+10;
const int MOD = 1e9+7;
ll z[3];
ll dp[MAXN][3];
int main()
{
int n,l,r;
scanf("%d%d%d",&n,&l,&r);
z[0]=(r)/3;
z[1]=(r)/3+((r)%3>=1?1:0);
z[2]=(r)/3+((r)%3>=2?1:0);
z[0]-=(l-1)/3;
z[1]-=(l-1)/3+((l-1)%3>=1?1:0);
z[2]-=(l-1)/3+((l-1)%3>=2?1:0);
dp[0][0]=1;
for(int i=1;i<=n;i++)
{
dp[i][0]=(dp[i-1][0]*z[0]%MOD+dp[i-1][1]*z[2]%MOD+dp[i-1][2]*z[1]%MOD)%MOD;
dp[i][1]=(dp[i-1][0]*z[1]%MOD+dp[i-1][1]*z[0]%MOD+dp[i-1][2]*z[2]%MOD)%MOD;
dp[i][2]=(dp[i-1][0]*z[2]%MOD+dp[i-1][1]*z[1]%MOD+dp[i-1][2]*z[0]%MOD)%MOD;
}
printf("%lld\n",dp[n][0]);
}