Codeforces Round #740 (Div. 2) D2. Up the Strip dp + 分块优化 + 逆向思维
傳送門
文章目錄
- 題意:
- 思路
題意:
有nnn個細胞,你初始在第nnn細胞上,假設你當前在xxx處,你每次可以進行如下兩個操作:
(1)(1)(1)選擇[1,x?1][1,x-1][1,x?1]內一個數yyy,跳到第x?yx-yx?y個細胞上。
(2)(2)(2)選擇[2,x][2,x][2,x]之間的一個數zzz,跳到?xz?\left \lfloor \frac{x}{z} \right \rfloor?zx??。
問你有多少種不同的方式到達111號細胞。
2≤n≤4e62\le n\le 4e62≤n≤4e6
思路
如果直接按照題意來設計狀態,f[i]f[i]f[i]表示從nnn到iii的方案數,那么第一個操作顯然可以用一個變量打一個標記,第二個可以整除分塊,將iii這個點的貢獻分配給?xz?\left \lfloor \frac{x}{z} \right \rfloor?zx??。
復雜度O(nn)O(n\sqrt n)O(nn?)
這個只能通過簡單版本,要通過這個題的話,顯然需要優化,其實不難發現他與倍數有關。
我們考慮倒著來設計狀態,f[i]f[i]f[i]表示從iii到111的方案數,那么f[1]=1f[1]=1f[1]=1。
假設當前枚舉的點是kkk,考慮?xz?=k\left \lfloor \frac{x}{z} \right \rfloor=k?zx??=k這個式子,代表從xxx點能到當前點kkk,由于我們是倒著來的,那么也就是kkk這個點可以轉移到xxx,考慮枚舉zzz,那么能轉移到的區間就是[k?z,k?z+z?1][k*z,k*z+z-1][k?z,k?z+z?1],通過枚舉倍數讓后打一個標記即可。
復雜度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] */總結
以上是生活随笔為你收集整理的Codeforces Round #740 (Div. 2) D2. Up the Strip dp + 分块优化 + 逆向思维的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 十六进制转八进制(浅显易懂)
- 下一篇: Unity3D 工程机械以及常见机构铰链