發表文章

目前顯示的是有「DP」標籤的文章

[AtCoder] [經典競程 90 題] 023 - Avoid War(★7)

題目連結: https://atcoder.jp/contests/typical90/tasks/typical90_w 題目大意: 給定一個 $H \times W$ 的棋盤,其中被標記為 # 的格子代表不能擺棋子。問有多少種方式可以在棋盤上擺一堆西洋棋的國王而不會造成至少一組國王可以立即攻擊到對方。 看完馬上想到 大根蘿蔔 ,這種題明顯要狀態壓縮 DP,狀態大概可以是 $dp[x][y][\text{mask}]$ 代表在 $(x, y)$ 的時候 $\text{mask}$ 裡面那些位置是可以擺的,然後轉移就真的看這格要不要擺之類的。 但如果真的無腦這樣做的話,複雜度會是 $O(H \times W \times 2^W)$ ,明顯會跑太久,不過仔細想了想就可以感受到合法狀態數絕對沒有這麼多,因為放一個就會蓋掉周遭之類的。 所以對於這種情況一個好方法就是我們可以先花 $W \times 2^W$ 的時間預處理真正合法的狀態,接著就可以 $O(H \times W \times X)$ (其中 $X$ 是真正可能的合法的狀態數) DP 掉了。 不過因為我很爛,從以前就一直不知道該怎麼好好寫這種預處理 DP,只知道這樣是可行的,所以這題最後跑去看 官解程式碼 才終於學會寫程式。 官解 同時也有稍微說明我上面的那個 $X$ 其實就是費式數列的某項,因為把蓋掉之後能再放的拿出來估一估差不多就會跟費式數列遞迴式一樣。 #include <bits/stdc++.h> using namespace std; static constexpr int C = 167761; static constexpr int N = 24; static constexpr int N2 = 1 << N; static constexpr int MOD = 1'000'000'007; inline int add(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; } string a[N]; unordered_map<int, pair...

[AtCoder] [經典競程 90 題] 019 - Pick Two(★6)

題目連結: https://atcoder.jp/contests/typical90/tasks/typical90_s 題目大意: 給定 $2N$ 個整數 $A_i$ ,現在可以花費 $|A_i - A_{i+1}|$ 的價格把相鄰兩項 $A_i$, $A_{i+1}$ 都刪掉。問把整個序列刪光的最小代價。 $N$ 很小,所以直接暴力 $O(N^4)$ DP 就好。$\text{DP}_{i, j}$ 代表把 $[i, j]$ 都刪掉的最小代價,轉移的話就直接枚舉刪光這個區間的最後一步是刪掉哪兩個數字就好了。 #include <bits/stdc++.h> using namespace std; const int INF = 1 << 29; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; n <<= 1; vector<int> a(n); for (int &ai : a) cin >> ai; vector<vector<int>> dp(n, vector<int>(n)); for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { dp[i][j] = INF; } } for (int len = 1; len < n; len += 2) { for (int i = 0; i + len < n; ++i) { // remove seg [i, j] const int j = i + len; for (int x = i; x ...

[AtCoder] [經典競程 90 題] 011 - Gravy Jobs(★6)

題目連結: https://atcoder.jp/contests/typical90/tasks/typical90_k 題目大意: 現在有 $N$ 個工作可以做,第 $i$ 個工作要在 $D_i$ 前做完,並且要花費 $C_i$ 天,如果有做完的話可以獲得 $S_i$ 元的報酬。一天只能做一個工作,且一個工作做了就不能中途換其他工作。問最多可以得到多少報酬。 假設最佳解是做了 $p_1$, $p_2$, $\cdots$, $p_m$ 這些工作,那我們按照死線先後順序來做這些工作並不會讓我們少做甚麼工作。這代表我們可以按照死線先後順序來挑工作做。考慮直接暴力 DP 要挑哪些工作,這裡 $\text{dp}_{i,j}$ 代表只做前 $i$ 個工作 (排好序後的前 $i$ 個) 的話,那第 $j$ 天時可以獲得多少報酬,轉移的話就直接在每個時間點都安排看看工作就好了。 #include <bits/stdc++.h> using namespace std; using lld = int64_t; constexpr int N = 5000 + 5; struct Task { int d, c; lld s; }; lld dp[2][N]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<Task> a(n); for (auto &a_i : a) { cin >> a_i.d >> a_i.c >> a_i.s; } sort(a.begin(), a.end(), [](const auto &x, const auto &y){ return x.d < y.d; }); int me = 0, he = ...

[AtCoder] [經典競程 90 題] 005 - Restricted Digits(★7)

題目連結: https://atcoder.jp/contests/typical90/tasks/typical90_e 題目大意: 給定 $c_i$ 代表可以用的數字種類,問在只用 $c_i$ 的情況下有多少 $N$ 位數是 $B$ 的倍數。 原本想暴力數位統計 DP 下去 ,但發現 $N$ 太大不能做,想了很久之後最後跑去看解答,我就爛= = 解答感覺也是一個很套路的東西,因為我們只要求 $N$ 位數而不是特定少於某個 $K$ 的數字,所以其實可以直接對 $N$ 倍增。初始狀態給的是 $1$ 位數的情況,那我們就可以倍增出 $2$ 位數的情況、$4$ 位數的情況、 $8$ 位數的情況...,對於 $N$ 位數,我們考慮它的二進位寫法,假設它是 $2^0 + 2^4 + 2^5$ 那我們就挑 $2^0$ 位數的情況、 $2^4$ 位數的情況、 $2^5$ 位數的情況來合併就好。 具體合併就是背包合併直接暴力枚舉高位跟低位分別模 $B$ 餘多少就好。 #include <bits/stdc++.h> using namespace std; using lld = int64_t; using llu = uint64_t; constexpr int M = 3000 + 5; constexpr int N = 64; constexpr int MOD = 1'000'000'007; static inline int add(int x, int y, int mod = MOD) { return x + y >= mod ? x + y - mod : x + y; } static inline void adde(int &x, int y, int mod = MOD) { x += y; if (x >= mod) x -= mod; } static inline int mul(int64_t x, int64_t y, int mod = MOD) { return static_cast<int>(x * y % mo...

[TIOJ] 1711. Apple Tree

題目連結: http://tioj.infor.org/problems/1711 說個笑話,oToToT會寫程式。 搞了好久都不會做,然後一直假解QAQ 去網路上查到momo學長的解居然也是錯的(詳見底下測資) 10 6 522 288 963 788 756 856 18 412 378 789 2 1 3 2 4 2 5 2 6 5 7 6 8 4 9 1 10 4 最後是去問了minson才知道一個怪怪的解QQ,我到現在還是不知道這為何是好的(他說複雜度是$O(N^2)$),可能跟CF 729F有類的概念吧。 註(2019/04/24):似乎是因為某種兩個東西只會在他們的LCA被合起來 總之就是用一個顯然的事實就是我們一個(子)樹最多只能走他底下size步,所以我們就只要維護size,並且每次上界都用當前大小做dp即可。 dp狀態也很簡單 $ dp[i][j][0] := \text{第i個節點走j步後且要走回來的最大值}, dp[i][j][1] := \text{第i個節點走j步後,停在他底下某個人的最大值} $轉移就直接暴力混和背包即可。 #include <cstdio> #include <cstring> #include <vector> #include <algorithm> const int N = 1000 + 5; int w[N], dp[N][N<<1][2]; int tmp[N<<1][2], sz[N]; std::vector<int> G[N]; void dfs(int,int,int); int main(){ int n, k; scanf("%d%d", &n, &k); k = std::min(k, n+n); for(int i=1;i<=n;i++) scanf("%d", w+i); for(int i=1;i<n;i++) { int u, v; ...

[TIOJ] 1914. 彩色紙條

題目連結: https://tioj.infor.org/problems/1914 搞了好久,才發現我一開始的DP式是錯的QQ 這題難點應該就是DP式吧,不過我也不知道我怎麼想到的,所以這邊就給式子就好:$dp[i][j] = \min_{i \leq k 艾佛森括號 ),然後$dp[i][j]$的意思是把$[i, j]$填滿的最小步數,然後轉移方法就是枚舉中點,代表先把$[i, k]$填好再填$[k+1, j]$,而減一的部份則是因為明顯若首尾相同,那我們在填左邊的時候就可以順便拉一條過去到右邊,就不需要右邊又在畫一遍了。 #include <bits/stdc++.h> using namespace std; const int N = 200 + 5; int arr[N], dp[N][N]; int main(){ int t; scanf("%d", &t); while(t--){ int n, m; scanf("%d%d", &n, &m); for(int i=1;i<=n;i++) scanf("%d", arr+i); for(int i=1;i<=n;i++) dp[i][i]=1; for(int i=n;i>=1;i--){ for(int j=i+1;j<=n;j++){ dp[i][j]=1<<25; for(int k=i;k+1<=j;k++) dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j]); if(arr[i]==arr[j]) dp[i][j]--; } } pr...

[Codeforces] 1051F. The Shortest Statement

題目連結: https://codeforces.com/contest/1051/problem/F 煩人的題目,一開始看到題的時候,忘記字串可以$(N, O(logN))$比較大小,然後想了想,想出一個什麼suffix array上二分搜之類的超噁比大小作法XD 後來發現可以$O(logN)$比大小之後,又因為hash碰撞卡了一段時間,才發現我p, q打反XD 總之,這題應該可以想到一個簡單的DP狀態$dp[i]$代表把後i個弄好的方法數,哪也很明顯的如果$s_i \neq \text{'0'}$,那就是找到一個區間的$j(i \le j \leq n)$使得$L \leq s_i...s_j \leq R$然後明顯$dp[i]=\sum_{j} dp[j]$,因為我們顯然只能把$s_i$跟那些人接在一起,而若$s_i = \text{'0'}$的情況也類似,所以現在變成我們要找到那些j,不過很明顯可以發現若a跟b差不多大,那他們的位數應該也差不多,所以我們只要去比較位數差不多的那個substring,就可以得知他是不是比較大了。 #include <bits/stdc++.h> using namespace std; const int N = 1000000 + 5; const int qs[] = { 1002939109, 1020288887, 1028798297, 1038684299, 1041211027, 1051762951, 1058585963, 1063020809, 1094763083, 1106384353, 1120154459, 1140593173, 1147930723, 1172520109, 1183835981, 1187659051, 1241251303, 1247184097, 1255940849, 1272759031, 1287027493, 1288511629, 1294632499, 1312650799, 1314753281, 1320080669, 1321970357, 1333133947, ...

[TIOJ] 1941. 直升機抓寶 (Helicopter)

題目連結: https://tioj.infor.org/problems/1941 這題應該可以輕鬆想到$O(N^2)$的DP方式,同時也會發現那樣子太慢。 仔細觀察DP的表格後,發現他是一個單調的表格,也就是由左至右遞增,同時由下至上遞增,接著研究一下每個轉移對DP表格的貢獻,發現他必定會將他自己那個區間的DP值+1,而後面則為了要維護單調的性質,會再跟他取max。 那這類題目,其實我們可以維護每一段的DP值,每次加入一個新的轉移來源後,也代表加入了一個斷點,但是同時也有可能把自身後面的斷點吃光光。 所以複雜度會是$O(N log N)$,因為每個斷點只會出現跟消失一次,而單次新增跟刪除了複雜度都是$O(log N)$ #include <bits/stdc++.h> using namespace std; class BIT{ private: int n; vector<int> arr; inline int lowbit(int x){return x&(-x);} void modify(int p, int x){ for(;p;p-=lowbit(p)) arr[p] += x; } public: void init(int n_){ n = n_; arr.clear(); arr.resize(n); } void modify(int l, int r, int v){ modify(l, -v); modify(r, v); } int query(int x){ int ret = 0; for(;x<n;x+=lowbit(x)) ret += arr[x]; ...

[TIOJ] 1067. C.互質任務

題目連結: http://tioj.infor.org/problems/1067 直接DP餘數即可,因為餘數$gcd(a, b) = gcd(a%b, b)$ #include <bits/stdc++.h> using namespace std; const int N = 1000 + 5; const int C = 10000 + 5; inline int gcd(int a, int b){return b?gcd(b, a%b):a;} int dp[2][C]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); bool me=0, he=1; int n, m; while(cin >> n >> m, n or m) { memset(dp[me], -1, sizeof(dp[me])); dp[me][0] = 0; for(int i=0;i<n;i++){ int x; cin >> x; for(int j=0;j<m;j++) if(dp[me][j] >= 0) { dp[he][j] = max(dp[he][j], dp[me][j]); dp[he][(j*10+x)%m] = max(dp[he][(j*10+x)%m], dp[me][j]+1); } memset(dp[me], -1, sizeof(dp[me])); swap(me, he); } int ans = 0; for(int i=0;i<m;i++) if(gcd(i, m) == 1){ ans...

[TIOJ] 1094. C.幼稚國王的獎賞

題目連結: http://tioj.infor.org/problems/1094 這題有三種作法,一種是我看完馬上想到的砍半枚舉後套trie #include <bits/stdc++.h> using namespace std; const int LOG_C = 20; const int N = 30 + 5; inline int two(int x){return 1<<x;} class Trie{ private: static constexpr int MEM = 5000000; struct node{ int lc, rc; node(): lc(0), rc(0){} } nodes[MEM]; int mem_, root; inline int new_node(){ assert(mem_ < MEM); nodes[mem_] = node(); return mem_++; } void insert(int x, int& cur, int d){ if(!cur) cur = new_node(); if(d == -1) return; if(x & two(d)) insert(x, nodes[cur].rc, d-1); else insert(x, nodes[cur].lc, d-1); } int query(int x, int cur, int d){ if(d == -1) return 0; if(x & two(...

[Codeforces] 909C. Python Indentation

題目連結: http://codeforces.com/problemset/problem/909/C 我不會DP QAQ 本題狀態可以訂成$dp[i][j]$代表到第$i$個statement時有$j$個intent的方法數,那轉移想了想後就可以得到,如果前面是for statement,那顯然現在的東西就只能擺在跟他同一個intent,而若前一個是普通的statement,則我們可以擺到intent比較小的任意一個位置。而這樣的話複雜度會變成$O(N^3)$,但是其實我們可以透過預處理前綴和以達到$O(N^2)$的複雜度。 #include <bits/stdc++.h> using namespace std; typedef long long lld; const int MOD = 1000000007; const int N = 5000 + 5; bool arr[N]; lld dp[N][N], sum[N]; int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int n; cin>>n; for(int i=1;i<=n;i++){ string ss; cin>>ss; if(ss[0]=='s') arr[i] = 1; else arr[i] = 0; } auto psum = [](int l, int r){ // 0-base [l, r] if(l) return sum[r]-sum[l-1]; else return sum[r]; }; dp[0][0] = 1; for(int i=1;i<=n;i++){ if(arr[i-1]){ sum[0] = dp[i-1][0]; for...

[AtCoder] CODE FESTIVAL 2017 qual C D: Yet Another Palindrome Partitioning

題目連結: http://code-festival-2017-qualc.contest.atcoder.jp/tasks/code_festival_2017_qualc_d 賽中唬爛了一個爛DP,很正常的TLE掉,賽後看著Editorial還是看不懂,只好去問別人QQ。 我用editorial的方式講一下好了:我們定義一種hash值$h(\text{str})=\sum_{i=0}^{26}2^i \cdot (sum(i) \mod 2)$其中$sum(i)$代表字元$i$出現幾次。這樣的話,我們會發現如果$\text{str}$是回文,若且唯若$h(\text{str})$是二的冪次或零(因為出現次數為奇的最多只能有一個),接下來我們幫$\text{str}$的前綴算好hash值($h_i$代表$[0, i)$的hash值),而另外一個小發現則是發現要算$\text{str}$中$[l, r)$的hash值,我們只要計算$h_l \oplus h_r$就好了。 那現在我們可以開始DP了,我們狀態是$dp[i]$代表$[0, i]$中我們至少要切幾段才能使得每塊都可以變回文,那我們會發現其實就是要找到最小的$dp[j]$使得$h_j \oplus h_i$是二的冪次或零,那這時我們再DP另外一個數字$val[S]$代表當$a_i=S$的最小$dp[i]$值,而因為xor的特性,我們只要枚舉$val[a_i], val[a_i \oplus 2^0], val[a_i \oplus 2^1] \cdots val[a_i \oplus 2^25]$就可以得到答案了(因為$(A \oplus B) \oplus A = B$),而得到答案後記得再更新一下$val[a_i]$即可。 #include <bits/stdc++.h> using namespace std; const int N = 200000 + 5; const int INF = 1<<30; string ss; int dp[1<<26], ans[N]; int main(){ cin>>ss; int n = ss.size(...

[Codeforces] 383D. Antimatter

題目連結: http://codeforces.com/problemset/problem/383/D 某種進階版的加減問題(?,原本的加減問題DP式大概長得像$dp[i][j] = dp[i-1][j+a_i]+dp[i-1][j-a_i]$之類的,而現在變成可以從任意時間點開始的話就除了原本的方法,還多了兩種:$dp[i][a_i]$跟$dp[i][-a_i]$ #include <bits/stdc++.h> using namespace std; const int C = 10000 + 5; const int N = 1000 + 5; const int MOD_BASE = 1000000007; #define dp(i, j) way[(i)][(j)+C] int way[N][2*C]; int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int n; cin>>n; for(int i=1;i<=n;i++){ int x; cin>>x; for(int j=10000;j>=-10000;j--){ if(j+x <= 10000) dp(i, j) += dp(i-1, j+x); if(j-x >= -10000) dp(i, j) += dp(i-1, j-x); dp(i, j) %= MOD_BASE; } dp(i, x)++; dp(i, -x)++; } int sum = 0; for(int i=1;i<=n;i++) sum = (sum+dp(i, 0))%MOD_BASE; cout<<sum<<'\n'; return 0; } ...

[TIOJ] 1589. 蚯蚓之入侵雅勒問題 Athena

題目連結: http://tioj.infor.org/problems/1589 很明顯的可以知道到一個點的方法數,就是到所有連到他的人的方法數加起來,所以我們可以用類似DP的方法記錄從起點到每個點的方法數,而DP順序就按照拓樸排序就好了XD #include <bits/stdc++.h> using namespace std; #define PB push_back typedef long long lld; const int N = 250 + 5; vector<int> G[N]; lld way[N]; int in[N]; int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int vv, ee, m; cin>>vv>>ee>>m; for(int i=0;i<ee;i++){ int u, v; cin>>u>>v; G[u].PB(v); in[v]++; } int st, ed; cin>>st>>ed; way[st]=1; queue<int> qq; for(int i=0;i<vv;i++) if(in[i]==0) qq.push(i); while(!qq.empty()){ int u = qq.front(); qq.pop(); for(auto v: G[u]){ way[v] = (way[v]+way[u])%m; in[v]--; if(in[v]==0) qq.push(v); } } cout<...

[Codeforces] 864E. Fire

題目連結: http://codeforces.com/problemset/problem/864/E 只感覺應該是DP,而且東東的順序會有影響,但是完全不知道該用何種順序來DP,看了tutorial才知道原來要按照消失時間來DP。 tutorial中其實並未給出證明,不過下面有人表示「如果你不先挑那些會先消失的人,那很有可能他待會就消失了(當然挑他要是有助益的)」,想了想我自己是覺得說,如果你可以先挑比較晚消失的人,再挑比較早消失的人,那你也一定可以先挑早消失再挑晚消失的,但反過來的挑法就不一定了。 那所以就按照那個順序排好序後,這題就變得類似於背包問題了,所以我們就可以跟背包一樣,用加入東東的方是想,每次加入一個物品就是在那些可行的範圍中往後加值之類的。 #include <bits/stdc++.h> using namespace std; const int N = 100 + 5; const int M = 2000 + 5; struct Obj{ int t, d, p; int id; inline bool operator<(const Obj &x) const { return d < x.d; } } arr[N]; int sum[M]; vector<int> ori[M]; int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int n; cin>>n; for(int i=0;i<n;i++){ cin>>arr[i].t>>arr[i].d>>arr[i].p; arr[i].id = i+1; } sort(arr, arr+n); for(int i=0;i<n;i++){ for(int j=arr[i].d-1;j>...

[Codeforces] 721C. Journey

題目連結: http://codeforces.com/problemset/problem/721/C 莫名其妙吃了MLE,只好把int改short, long long改int = = 本題的做法滿DP的,我狀態直接訂成$dp[i][j]$代表起點到$i$號節點,經過$j$個中繼點所需的最短時間,而DP的順序就按照圖的拓樸排序順序DP就可以好好DP了,不過要注意的是起點有可能不會是拓樸排序的第一個點,所以要稍微判一下... #include <cstdio> #include <vector> #include <utility> #include <stack> #include <queue> using namespace std; typedef long long lld; #define PB push_back typedef pair<short,int> PSI; typedef pair<short,short> PSS; #define FF first #define SS second const int N = 5000 + 5; const int INF = 1<<30; short in[N]; int dp[N][N]; PSS ori[N][N]; vector<PSI> G[N]; int main(){ short n, m;int t; scanf("%hd%hd%d",&n,&m,&t); fill(dp[0], dp[n]+n+1, INF); for(int i=0;i<m;i++){ short u, v; int c; scanf("%hd%hd%d",&u,&v,&c); G[u].PB({v, c}); in[v]++; ...

[TIOJ] 1021. G.Counting Page Numbers

題目連結: http://tioj.infor.org/problems/1021 這裡每個狀態存的是在這個狀態下有幾個k出現,而若現在再加一個k就是再加入前面數字的數量,所以我還先算了一下每個情況數字的數量。(我不太會數位DP,講的可能不是很好OAO #include <bits/stdc++.h> using namespace std; typedef long long lld; const int LogN = 10 + 5; int dig[LogN], k, step; lld cnt[LogN][2][2], dp[LogN][2][2]; int cnted[LogN][2][2], dped[LogN][2][2]; lld calc(int,bool,bool); lld go(int,bool,bool); int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int n; while(cin>>n>>k){ step++; int logN; for(logN = 0;n>0;logN++){ dig[logN] = n%10; n/=10; } calc(logN, 0, 0); cout<<go(logN, 0, 0)<<'\n'; } return 0; } lld calc(int pos, bool any, bool start){ if(pos < 0){ cnt[pos+1][any][start]= start; return start; } if(cnted[pos+1][any][st...

[Codeforces] 855C. Helga Hufflepuff's Cup

題目連結: http://codeforces.com/contest/855/problem/C 賽中看到一堆大大們都寫出來的題目,但是自己怎麼寫都寫不出來就是了QQ,一定是因為我DP太爛的緣故 總之這題很明顯就是個樹DP題,狀態這裡是定成$$ dp[i][x][0] := 第i個節點放[1,k-1]種類且有x個k的方法數 \\ dp[i][x][1] := 第i個節點放k且有x個k的方法數 \\ dp[i][x][2] := 第i個節點放[k+1,M]且有x個k的方法數 $$轉移的話就算滿好想的了,大致就是0號可以從0,1,2轉來;2只能從0轉來;ㄉ可以從0, 2轉來,而怎麼轉就直接枚舉其中一邊有幾個就好了。 #include <bits/stdc++.h> using namespace std; #define PB push_back typedef long long lld; const int N = 100000 + 5; const int K = 10 + 2; const int MOD_BASE = 1e9+7; int n, m, k, x; lld dp[N][K][3]; vector<int> G[N]; void dfs(int,int); int main(){ ios_base::sync_with_stdio(0);cin.tie(0); cin>>n>>m; for(int i=0;i<n-1;i++){ int u, v; cin>>u>>v; G[u].PB(v); G[v].PB(u); } cin>>k>>x; dfs(1, 1); lld ans = 0; for(int i=0;i<=x;i++){ ans=(ans+dp[1][i][0])%MOD_BASE;...

[TIOJ] 1997. 數列切割

題目連結: http://tioj.infor.org/problems/1997 校內複賽的時候只喇到$k \leq 3$的測資,不過感覺這題是DP,只是賽中一直想不太出來,後來被雷了才知道這題的做法... 狀態設成$dp[i][j]$代表用前$i$個湊出$j$組的最大值可能為多少,而很明顯知道若是現在再加一個數,可以選擇的就是變成$dp[i+1][j+1]$或$dp[i+1][j]$,所以就照著這DP下去就好了,然後記得判一下奇偶之類的。 #include <bits/stdc++.h> using namespace std; typedef long long lld; typedef pair<int,int> PII; #define FF first #define SS second const int N = 1000000 + 5; const lld INF = 1LL<<40; int arr[N]; lld dp[N][7]; PII ori[N][7]; int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int n, k; cin>>n>>k; for(int i=1;i<=n;i++) cin>>arr[i]; fill(dp[1], dp[n]+7, -INF); dp[1][1]=arr[1]; for(int j=1;j<=k;j++){ for(int i=j;i<n;i++){ if(j&1){ if(dp[i][j]+arr[i+1] > dp[i+1][j]){ dp[i+1][j] = dp[i][j]+arr[i+1]; ori[i+1][j] = {i, j};...

[Codeforces] E. Salazar Slytherin's Locket

題目連結: http://codeforces.com/contest/855/problem/E 早上才學完數位統計DP晚上就寫不出來,我還真垃圾... 這題我的狀態是定成$dp[i][j][k]$代表在$i$進位下,算到第$j$位,而且有$k$個奇數次時的方法數,這狀態的轉移很簡單,就是枚舉多一位奇數或少一位奇數($dp[i][j][k]=dp[i][j-1][k-1] \cdot k + dp[i][j-1][k+1] \cdot (i-k)$)。那這樣的話,對於每一個範圍,我們就只要枚舉他在哪一位開始可以任意選之類的就可以回答了。 #include <bits/stdc++.h> using namespace std; typedef long long lld; const int N = 65; int dig[N]; lld dp[11][N][11]; bool dped[11][N][11]; void init(); lld go(int,int,bool,int,bool); lld calc(int,int,int); int main(){ ios_base::sync_with_stdio(0);cin.tie(0); init(); int q; cin>>q; while(q--){ int b; cin>>b; long long l, r;cin>>l>>r; l--; int cnt; for(cnt=0;l>0;cnt++){ dig[cnt]=l%b; l/=b; } dig[cnt]=0; lld L = go(b, cnt, 0, 0, 1); for(cnt=0;r>0;cnt++){ ...