首页 > 其他分享 >CodeForces - 1485F

CodeForces - 1485F

时间:2024-07-15 19:52:58浏览次数:9  
标签:前缀 sum CodeForces tot pos 1485F 数组 dp

题目大意

给定数组 \(b\),求有多少 \(a\) 数组满足 \(a_i=b_i \ or\ \sum\limits_{k=1}^i a_k=b_i\)。

分析

既然有前缀和,不妨将前缀和计入状态中,设 \(dp_{i,j}\) 为前 \(i\) 个前缀和为 \(j\) 的方案数。
考虑两种条件的转移方程。

  • 若选第一种,有 \(dp_{i,j}=dp_{i-1,j-b_i}\)
  • 若选第二种,有 \(dp_{i,b_i}=\sum\limits_{k=-\infty}^{+\infty} dp_{i-1,k}\)

此处 \(dp_{i,b_i}\) 会多算一次 \(dp_{i-1,0}\),需要减去。
直接实现这个 \(dp\) 显然不现实,挖掘其中性质,发现第一种条件即为将原本的数列平移,第二种也只是单点赋值。

由上,考虑记录一个 \(pos\) 为数组偏移度,一个 \(tot\) 为总方案数。
那么显然 \(dp_{i,b_i}\) 即为 \(tot\),每次动态更新 \(tot\) 即可

代码


map <int,int> f;

void Main(){
	f.clear();
	f[0]=tot=1;
	n=rd,pos=0;
	for(int i=1;i<=n;i++){
		b=rd,pos+=b;
		int x=(tot-f[b-pos]+mod)%mod;
		f[b-pos]=tot;
		tot=(tot+x)%mod;
	}
	cout<<tot<<endl;
}

标签:前缀,sum,CodeForces,tot,pos,1485F,数组,dp
From: https://www.cnblogs.com/smilemask/p/18303858

相关文章

  • Codeforces Round #956 (Div. 2) and ByteRace 2024
    目录写在前面ABCDEF写在最后写在前面比赛地址:https://codeforces.com/contest/1983孩子们我回来了。这场实在是太对胃口,vp不到1h就4题了之后EF也口出来了,然而赛时睡大觉去了没打真是亏死。感觉自己的文字能力已经很牛逼了,不需要再多练了,以后的题解都会写得简略些。A......
  • codeforces 1980 E. Permutation of Rows and Columns
    题目链接https://codeforces.com/problemset/problem/1980/E题意共输入\(T\)组测试用例,每组测试用例第一行输入两个整数\(n,m\),分别代表输入的数据的行数和列数,其中\(n*m\leq2*10^5\)。接下来输入两个\(n\)行\(m\)列的矩阵\(a,b\),对于每个矩阵中的元素\(x_{i,j}\)都是......
  • Solution - Codeforces 1311E Construct the Binary Tree
    先去考虑找一下无解条件。首先就是有\(d\)关于\(n\)的下界\(L\),就是弄成一颗完全二叉树的答案。其次有\(d\)关于\(n\)的上界\(R\),就是成一条链的样子。首先当\(d<L\)或\(R<d\)时显然无解。对于\(L\led\leR\)又如何去判定。能发现没有一个比较好的判定......
  • 题解:CodeForces 346A Alice and Bob[博弈/数论]
    CodeForces346AA.AliceandBobtimelimitpertest2secondsmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputItissoboringinthesummerholiday,isn'tit?SoAliceandBobhaveinventedanewgametoplay.Therulesa......
  • 题解:CodeForces 843A Sorting by Subsequences[模拟/排序]
    CodeForces843AA.SortingbySubsequencestimelimitpertest:1secondmemorylimitpertest:256megabytesinputstandardinputoutputstandardoutputYouaregivenasequence\(a_1, a_2, ..., a_n\)consistingofdifferentintegers.Itisrequiredtos......
  • 题解:CodeForces 618C Constellation[贪心/模拟]
    CodeForces618CC.Constellationtimelimitpertest:2secondsmemorylimitpertest:256megabytesinputstandardinputoutputstandardoutputCatNokuhasobtainedamapofthenightsky.Onthismap,hefoundaconstellationwithnstarsnumberedfrom......
  • 题解:CodeForces 835 D Palindromic characteristics[区间dp/马拉车Manacher]
    CodeForces835DD.Palindromiccharacteristicstimelimitpertest:3secondsmemorylimitpertest:256megabytes*inputstandardinputoutputstandardoutputPalindromiccharacteristicsofstring\(s\)withlength\(|s|\)isasequenceof\(|s|\)in......
  • 题解:CodeForces 1019 A Elections[贪心/三分]
    CodeForces1019AA.Electionstimelimitpertest:2secondsmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputAsyouknow,majorityofstudentsandteachersofSummerInformaticsSchoolliveinBerlandforthemostparto......
  • 题解:CodeForces 1511 C Yet Another Card Deck[暴力/模拟]
    CodeForces1511CC.YetAnotherCardDeckDescriptionYouhaveacarddeckof\(n\)cards,numberedfromtoptobottom,i. e.thetopcardhasindex\(1\)andbottomcard —index\(n\).Eachcardhasitscolor:the\(i\)-thcardhascolor\(a_i\......
  • Codeforces 956 Div2
    期末考试结束,开始训练A.ArrayDivisibility----------------------------------题解----------------------------简单的构造题,要让数组a里面的下表为1<=k<=n的数以及下表为(k的因数)的数加起来的和能被K整除,那我们只需要让每一个k的因数都能被k整除就行了,直接让每一个编号i......