Ayoub and Lost Array
time limit per test 1 second
memory limit per test 256 megabytes
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.
Input
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.
Output
Print the remainder when dividing by 109+7109+7 the number of ways to restore the array.
Examples
input
2 1 3
output
3
input
3 2 2
output
1
input
9 9 99
output
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].
题目大意:输入n l r三个整数,让你求有多少个长度为n的序列,序列的元素大小处于[l,r]之间,并且序列所有元素的和sum%3==0,就是能被3整除。结果模1e9+7。
题目解析:通过l,r可以计算出[l,r]之间有多少个数 __%3==1、__%3==2、__%3==0。然后定义一个数组x[3],保存当前和 sum模3为0,为1,为2的个数。然后从头更新一遍输出就可以了。
具体看代码:
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define ll long long
const ll man=2e5+50;
const ll mod=1e9+7;
using namespace std;
ll x[3],y[3];
ll n,l,r,a[3];
void init()//求有多少个元素模3等于0,1,2,保存在数组a[]中
{
x[0]=(l-1)/3;
y[0]=r/3;
x[1]=x[2]=x[0];
y[1]=y[2]=y[0];
if((l-1)%3==2) x[1]++,x[2]++;
else if((l-1)%3==1)x[1]++;
if(r%3==2) y[1]++,y[2]++;
else if(r%3==1)y[1]++;
a[0]=y[0]-x[0];
a[1]=y[1]-x[1];
a[2]=y[2]-x[2];
}int main()
{
cin>>n>>l>>r;
init();
memset(x,0,sizeof x);
memset(y,0,sizeof y);
x[0]=a[0],x[1]=a[1],x[2]=a[2];
for(int i=2;i<=n;i++){
y[0]=((x[0]*a[0])%mod+(x[1]*a[2])%mod+(x[2]*a[1])%mod)%mod;
y[1]=((x[0]*a[1])%mod+(x[1]*a[0])%mod+(x[2]*a[2])%mod)%mod;
y[2]=((x[0]*a[2])%mod+(x[1]*a[1])%mod+(x[2]*a[0])%mod)%mod;
x[0]=y[0];x[1]=y[1];x[2]=y[2];//x和y数组不断交替更新
}
cout<<x[0]<<endl;
return 0;
}