Codeforces 1105C Ayoub and Lost Array

题目大意:

  一个长度为$n$的数组,其和能被$3$整除,且每一个数字满足$a_{i}\in [l,r]$,问有多少种可以满足上述三个条件的数组

分析:

  $dp$。$dp[i][j]=$前$i$个数构成余数为$j$的方案数,然后通过这个$dp$的定义,可以推出递推方程$dp[i][j]=\sum_{i}^{2}dp[i][(j+k)%3]*n[k]$,其中$n[k]$为满足数字$a_{i}\in [l,r]$余数为$k$的个数,而$n[k]$的求法也很简单,以余数为$1$为例,假设$a_{i}=3\times m +1$,容易得到$l\leq 3\times m+1\leq r$,即$\frac{l-1}{3}\leq k\leq \frac{r-1}{3}$,而其间的个数有为$\lfloor  \frac{l-2}{3} \rfloor -\lfloor  \frac{r-1}{3} \rfloor$,但是由于$-2$可能会出现$-1$或者$-2$,会影响到范围,所以我们两边都加上$3$,于是就变成了 $\lfloor  \frac{l+1}{3} \rfloor -\lfloor  \frac{r+2}{3} \rfloor$,另外两种情况计算同上。

code:

#define debug
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e7;
const int MAXN = 1e3 + 10;
const ll INF = 0x3f3f3f3f;
const ll inf = 0x7fffff;
const ll mod = 1e9 + 7;
const int MOD = 10007;
ll dp[maxn][3];
void solve() {
	ll n,l,r;
	cin>>n>>l>>r;
	ll nn[3]= {r/3-(l-1)/3,(r+2)/3-(l+1)/3,(r+1)/3-(l)/3};
	dp[0][0]=1;
	for(int i=1; i<n+1; i++) {
		for(int j=0; j<3; j++) {
			for(int k=0; k<3; k++)dp[i][j]+=dp[i-1][(j+k)%3]*nn[(3-k)%3]%mod;
			dp[i][j]%=mod;
		}
	}
	cout<<dp[n][0]<<endl;
	memset(dp,0,sizeof(dp));
}
int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef debug
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	int T=1;
//	cin>>T;
	while(T--)
		solve();
	return 0;
}

  

原文地址:https://www.cnblogs.com/visualVK/p/10343518.html

时间: 2024-08-06 08:24:00

Codeforces 1105C Ayoub and Lost Array的相关文章

Codeforces Round #533 (Div. 2)C. Ayoub and Lost Array

C. Ayoub and Lost Array Ayoub had an array ?? of integers of size ?? and this array had two interesting properties: All the integers in the array were between ?? and ?? (inclusive). The sum of all the elements was divisible by 3. Unfortunately, Ayoub

Educational Codeforces Round 23 D. Imbalanced Array(单调栈)

题目链接:Educational Codeforces Round 23 D. Imbalanced Array 题意: 给你n个数,定义一个区间的不平衡因子为该区间最大值-最小值. 然后问你这n个数所有的区间的不平衡因子和 题解: 对每一个数算贡献,a[i]的贡献为 当a[i]为最大值时的 a[i]*(i-l+1)*(r-i+1) - 当a[i]为最小值时的a[i]*(i-l+1)*(r-i+1). 计算a[i]的l和r时,用单调栈维护.具体看代码,模拟一下就知道了. 然后把所有的贡献加起来.

codeforces 442C C. Artem and Array(有深度的模拟)

题目 感谢JLGG的指导! 思路: //把数据转换成一条折线,发现有凸有凹 //有凹点,去掉并加上两边的最小值//无凹点,直接加上前(n-2)个的和(升序)//数据太大,要64位//判断凹与否,若一边等于,一边大于,那中间这个也算是凹进去的,所以判断时要加上等于 //有凹点,去掉并加上两边的最小值 //无凹点,直接加上前(n-2)个的和(升序) //数据太大,要64位 //判断凹与否,若一边等于,一边大于,那中间这个也算是凹进去的,所以判断时要加上等于 #include<stdio.h> #i

Codeforces 220B - Little Elephant and Array 离线树状数组

This problem can be solve in simpler O(NsqrtN) solution, but I will describe O(NlogN) one. We will solve this problem in offline. For each x (0?≤?x?<?n) we should keep all the queries that end in x. Iterate that x from 0 to n?-?1. Also we need to kee

CF1105C Ayoub and Lost Array ——动态规划

CF1105C Ayoub and Lost Array 题意:一个整数数组,满足: 1. 长度为n 2. 所有元素都在[l, r]范围内 3. 所有元素的和能被3整除给出n, l, r (1 ≤ n ≤ 2*10^5,1 ≤ l ≤ r ≤ 10^9)请找出符合条件的数组的个数,答案对 10^9 + 7取模 首先我们要处理出[l, r]中对3取模得结果分别为0,1,2的数的个数,在一个合乎要求的数组中,结果为1和2的数的个数必然一样,由此就可以很方便地得到所有可能的组合的个数.但新的问题来了,

C. Ayoub and Lost Array cf dp

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 aa of integers of size nn and this array had two interesting properties: All the integers in the a

codeforces round 533 div2 C Ayoub and Lost Array [dp]

一道思维题 不仅是和这道题在战斗,我和编译器也进行了一场激烈的角逐 因为编译器出了点小问题... 对于dev或者codeblocks 我的方法是卸载了重新装/重启电脑 但是对于vscode 我的方法是, 对着它掉眼泪,看它能不能可怜可怜我,赶紧恢复到正常状态.... #include<bits/stdc++.h> using namespace std; typedef long long ll; //#define int long long const ll N = 2e5 + 1000;

CodeForces 221D Little Elephant and Array

Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 221D64-bit integer IO format: %I64d      Java class name: (Any) The Little Elephant loves playing with arrays. He has array a,

【Codeforces 258B】 Sort the Array

[题目链接] http://codeforces.com/contest/451/problem/B [算法] 模拟 在序列中找到一段单调递增的子序列,将这段序列反转,然后判断序列是否变得单调递增,即可 [代码] #include<bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; int i,n,l,r; bool flag; int a[MAXN]; int main() { scanf("%d"