Codeforces 1105C Ayoub and Lost Array

本文探讨了Ayoub丢失的数组问题,旨在寻找满足特定条件的整数序列数量。序列长度固定,元素范围限定,总和需被3整除。通过分析元素模3的特性,设计算法高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值