筛法应用CF1558B - Up the Strip

Posted 出尘呢

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了筛法应用CF1558B - Up the Strip相关的知识,希望对你有一定的参考价值。

https://codeforces.com/contest/1558/problem/B
为什么要2*f[i-1]
一个f[i-1]是f[i]–>f[i-1]
一个f[i-1]是因为f[i-1]是sum(f[1~i-2])+筛法中连续的一块用第一个代替的叠加影响力
以f[6]为例
f[6]=f[5,4,3,2,1]+f[6/2,6/3,6/4,6/5,6/6]
6,
6/2是3筛的,加f[3],同时-f[2]表示/2是2的日子到头了
6/3是2筛的,加f[2],同时-f[1]表示/3是1的日子到头了
6/4是1筛的,加f[1],同时-f[0]表示/4是0的日子到头了
6/5,6/6同上
而f[5]=f[4,3,2,1]+f[5/2,5/3,5/4,5/5]
它后面带着f[2,1,1,1]
而到6,也就是
f[6]=f[5]+f[4,3,2,1]+f[2改3,1改2,1,1,新加/6=1]
就是这样动态维护的!

为什么要f[2]–
f[1]无法用f的表达式表示了,只能特判,有的时候是要变换策略的

#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
FILE*FP=freopen("text.in","r",stdin);
#endif
#define int long long
#define N 4000006
int n,m,f[N];
signed main()
{
	scanf("%lld%lld",&n,&m);
	
	for(int i=1;i<=n;i++){
		f[i]=(f[i]+2*f[i-1])%m;
		f[1]=1;
		if(i==2)f[i]--;
		for(int j=2*i;j<=n;j+=i){
			f[j]=(f[j]+f[i]-f[i-1])%m;
		}
		for(int i=1;i<=n;i++){
			printf("%4d",f[i]); 
		}putchar('\\n');
	}
}

以上是关于筛法应用CF1558B - Up the Strip的主要内容,如果未能解决你的问题,请参考以下文章

筛法应用CF1558B - Up the Strip

CF1065F Up and Down the Tree

CF1065F Up and Down the Tree

[CF1065F]Up and Down the Tree

cf1561D Up the Strip(D1&&D2)

CF1096E The Top Scorer 组合数 容斥