Codeforces Round #740 (Div. 2) D2. Up the Strip dp + 分块优化 + 逆向思维
傳送門
文章目錄
- 題意:
- 思路
題意:
有nnn個(gè)細(xì)胞,你初始在第nnn細(xì)胞上,假設(shè)你當(dāng)前在xxx處,你每次可以進(jìn)行如下兩個(gè)操作:
(1)(1)(1)選擇[1,x?1][1,x-1][1,x?1]內(nèi)一個(gè)數(shù)yyy,跳到第x?yx-yx?y個(gè)細(xì)胞上。
(2)(2)(2)選擇[2,x][2,x][2,x]之間的一個(gè)數(shù)zzz,跳到?xz?\left \lfloor \frac{x}{z} \right \rfloor?zx??。
問(wèn)你有多少種不同的方式到達(dá)111號(hào)細(xì)胞。
2≤n≤4e62\le n\le 4e62≤n≤4e6
思路
如果直接按照題意來(lái)設(shè)計(jì)狀態(tài),f[i]f[i]f[i]表示從nnn到iii的方案數(shù),那么第一個(gè)操作顯然可以用一個(gè)變量打一個(gè)標(biāo)記,第二個(gè)可以整除分塊,將iii這個(gè)點(diǎn)的貢獻(xiàn)分配給?xz?\left \lfloor \frac{x}{z} \right \rfloor?zx??。
復(fù)雜度O(nn)O(n\sqrt n)O(nn?)
這個(gè)只能通過(guò)簡(jiǎn)單版本,要通過(guò)這個(gè)題的話,顯然需要優(yōu)化,其實(shí)不難發(fā)現(xiàn)他與倍數(shù)有關(guān)。
我們考慮倒著來(lái)設(shè)計(jì)狀態(tài),f[i]f[i]f[i]表示從iii到111的方案數(shù),那么f[1]=1f[1]=1f[1]=1。
假設(shè)當(dāng)前枚舉的點(diǎn)是kkk,考慮?xz?=k\left \lfloor \frac{x}{z} \right \rfloor=k?zx??=k這個(gè)式子,代表從xxx點(diǎn)能到當(dāng)前點(diǎn)kkk,由于我們是倒著來(lái)的,那么也就是kkk這個(gè)點(diǎn)可以轉(zhuǎn)移到xxx,考慮枚舉zzz,那么能轉(zhuǎn)移到的區(qū)間就是[k?z,k?z+z?1][k*z,k*z+z-1][k?z,k?z+z?1],通過(guò)枚舉倍數(shù)讓后打一個(gè)標(biāo)記即可。
復(fù)雜度O(nlogn)O(nlogn)O(nlogn)
// Problem: D1. Up the Strip (simplified version) // Contest: Codeforces - Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine)) // URL: https://codeforces.com/contest/1561/problem/D1 // Memory Limit: 128 MB // Time Limit: 6000 ms // // Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native") //#pragma GCC optimize(2) #include<cstdio> #include<iostream> #include<string> #include<cstring> #include<map> #include<cmath> #include<cctype> #include<vector> #include<set> #include<queue> #include<algorithm> #include<sstream> #include<ctime> #include<cstdlib> #include<random> #include<cassert> #define X first #define Y second #define L (u<<1) #define R (u<<1|1) #define pb push_back #define mk make_pair #define Mid ((tr[u].l+tr[u].r)>>1) #define Len(u) (tr[u].r-tr[u].l+1) #define random(a,b) ((a)+rand()%((b)-(a)+1)) #define db puts("---") #define lowbit(x) (x&(-x)) using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); } //void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); } //void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII;const int N=5000010,INF=0x3f3f3f3f; const double eps=1e-6;int n,mod; LL a[N],f[N];int main() { // ios::sync_with_stdio(false); // cin.tie(0);scanf("%d%d",&n,&mod);f[1]=1; LL add=0;for(int i=1;i<=n;i++) {a[i]+=a[i-1]; a[i]%=mod;f[i]=add+f[i]+a[i]; f[i]%=mod;add+=f[i]; add%=mod;for(int j=2;1ll*j*i<=n;j++) {int l=j*i,r=j*i+j-1; r=min(r,n+1);a[l]+=f[i]; a[r+1]-=f[i];a[l]%=mod; a[r+1]%=mod; a[r+1]+=mod; a[r+1]%=mod;}}cout<<f[n]%mod<<endl;return 0; } /*1 2 3 4 5 6 7 8 1-> 2 3 4 5 6 7 8 2-> [4,5] */總結(jié)
以上是生活随笔為你收集整理的Codeforces Round #740 (Div. 2) D2. Up the Strip dp + 分块优化 + 逆向思维的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 十六进制转八进制(浅显易懂)
- 下一篇: Codeforces Round #74