传送门:http://codeforces.com/contest/1105/problem/C
C. Ayoub and Lost Array
1 second
256 megabytes
standard input
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 ll and rr (inclusive).
- The sum of all the elements was divisible by 33.
Unfortunately, Ayoub has lost his array, but he remembers the size of the array nn and the numbers ll and rr, 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+7109+7 (i.e. the remainder when dividing by 109+7109+7). In case there are no satisfying arrays (Ayoub has a wrong memory), print 00.
The first and only line contains three integers nn, ll and rr (1≤n≤2⋅105,1≤l≤r≤1091≤n≤2⋅105,1≤l≤r≤109) — the size of the lost array and the range of numbers in the array.
Print the remainder when dividing by 109+7109+7 the number of ways to restore the array.
2 1 3
3
3 2 2
1
9 9 99
711426616
In the first example, the possible arrays are : [1,2],[2,1],[3,3][1,2],[2,1],[3,3].
In the second example, the only possible array is [2,2,2][2,2,2].
题意概括:
要求构造一个长度为 N 的序列,
要求:
1、序列里的数由 【L, R】区间里的数构成。
2、序列里的数值和要能整除 3
解题思路:
一开始还傻傻地以为有什么神奇的规律.....
其实是一道 DP
状态: dp[ i ][ k ] 累积到当前序列第 i 位的数值和 余 k 的方案数
因为要能整除 3 ,所以 k 只能取 0, 1, 2;
sumi 为 区间 【L,R】的模 3 == i 的值的数量
转移方程:
dp[ i ][ 0 ] = dp[i-1][0]*sum0 + dp[i-1][1]*sum2 + dp[i-1][2]*sum1;
dp[ i ][ 1 ] = dp[i-1][0]*sum1 + dp[i-1][1]*sum0 + dp[i-1][2]*sum2;
dp[ i ][ 2 ] = dp[i-1][0]*sum2 + dp[i-1][1]*sum1 + dp[i-1][2]*sum0;
AC code:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const LL MOD = 1e9+;
const int MAXN = 2e5+;
LL ans;
LL dp[MAXN][]; int main()
{
LL N, L, R;
LL it0 = , it1 = , it2 = ;
scanf("%I64d %I64d %I64d", &N, &L, &R);
LL len = R-L+;
LL c = len/3LL, d =len%3LL;
it0 = c; it1 = c; it2 = c;
if(d){
LL t = d==?:;
if(L%==) it0++, it1+=t;
else if(L% == ) it1++, it2+=t;
else it2++,it0+=t;
} dp[][] = it0;
dp[][] = it1;
dp[][] = it2; for(int i = ; i <= N; i++){
dp[i][] = ((dp[i-][]*it0)%MOD + (dp[i-][]*it2)%MOD + (dp[i-][]*it1)%MOD)%MOD; dp[i][] = ((dp[i-][]*it0)%MOD + (dp[i-][]*it1)%MOD + (dp[i-][]*it2)%MOD)%MOD; dp[i][] = ((dp[i-][]*it0)%MOD + (dp[i-][]*it1)%MOD + (dp[i-][]*it2)%MOD)%MOD; } printf("%I64d\n", dp[N][]%MOD);
return ; }