C. Ayoub and Lost Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Ayoub had an array ?a of integers of size ?n 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 33.
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+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.
Input
The first and only line contains three integers ?n, ?l and ?r (1≤?≤2⋅105,1≤?≤?≤1091≤n≤2⋅105,1≤l≤r≤109) — 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][1,2],[2,1],[3,3].
In the second example, the only possible array is [2,2,2][2,2,2].
题意:在【l, r】内找n个数(可重复),使得sum是3的倍数。
题解:简单dp,n0,n1,n2分别表示在【l,r】中除以3余0,1,2数的个数
转移方程:
dp[i][0] = (dp[i-1][0] * n0 + dp[i-1][1] * n2 + dp[i-1][2] * n1) % mod;
dp[i][1] = (dp[i-1][0] * n1 + dp[i-1][1] * n0 + dp[i-1][2] * n2) % mod;
dp[i][2] = (dp[i-1][0] * n2 + dp[i-1][1] * n1 + dp[i-1][2] * n0) % mod;
初始化:dp[0][0] = 1; dp[0][1] = 0; dp[0][2] = 0;
//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<sstream>
#include<iterator>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node<<1
#define rson mid + 1, r, node<<1|1
const int INF = 0x3f3f3f3f;
const int O = 1e6;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 5;
const double PI = acos(-1.0);
const double E = 2.718281828459;
int main(){
int n, l, r; scanf("%d%d%d", &n, &l, &r);
LL dp[maxn][3]; dp[0][0] = 1; dp[0][1] = 0; dp[0][2] = 0;
int n0 = r /3 - (l -1) /3;
int n1 = (r +2) /3 - ((l -1) +2) /3;
int n2 = (r +1) /3 - ((l -1) +1) /3;
for(int i=1; i<=n; i++) {
dp[i][0] = (dp[i-1][0] * n0 + dp[i-1][1] * n2 + dp[i-1][2] * n1) % mod;
dp[i][1] = (dp[i-1][0] * n1 + dp[i-1][1] * n0 + dp[i-1][2] * n2) % mod;
dp[i][2] = (dp[i-1][0] * n2 + dp[i-1][1] * n1 + dp[i-1][2] * n0) % mod;
}
printf("%lld\n", dp[n][0]);
return 0;
}