不到26位就加上一個最小的
到26位了就搜一下,最多回溯就一次,因此複雜度不大ios
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <ctime> #include <vector> //#define ivorysi #define MAXN 100005 #define eps 1e-7 #define mo 974711 #define pb push_back #define mp make_pair using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; char s[55],ans[55]; int N,L; bool vis[55]; bool dfs(int dep) { if(dep > 26) return false; if(!vis[s[dep] - 'a']) { vis[s[dep] - 'a'] = 1; ans[dep] = s[dep]; if(dfs(dep + 1)) return true; vis[s[dep] - 'a'] = 0; } for(int i = s[dep] - 'a' + 1 ; i <= 25 ; ++i) { if(!vis[i]) { L = dep; ans[dep] = 'a' + i; return true; } } return false; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif scanf("%s",s + 1); N = strlen(s + 1); if(N != 26) { for(int i = 1 ; i <= N ; ++i) vis[s[i] - 'a'] = 1; for(int i = 1 ; i <= N ; ++i) putchar(s[i]); for(int i = 0 ; i <= 25 ; ++i) { if(!vis[i]) { putchar('a' + i); break; } } putchar('\n'); } else { if(!dfs(1)) puts("-1"); else { for(int i = 1 ; i <= L ; ++i) putchar(ans[i]); putchar('\n'); } } }
3 4 5都特判輸出
根據30000咱們猜想每三個數裏要有兩個,或者每六個數裏有四個
咱們根據這些數%6的餘數分紅0 2 3 4 ,6減去他們和他們本身都不互質,因此考慮這麼構造c++
可是咱們但願咱們的總和是6的倍數,咱們算出當前%6的餘數再修改某個點使得總和是6的倍數便可spa
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <ctime> #include <vector> //#define ivorysi #define MAXN 20005 #define eps 1e-8 #define mo 974711 #define pb push_back #define mp make_pair #define pii pair<int,int> #define fi first #define se second using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; const int64 MOD = 998244353; int N; int c = 0,ans[MAXN],S; int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif scanf("%d",&N); if(N == 3) puts("2 5 63"); else if(N == 4) puts("2 5 20 63"); else if(N == 5) puts("2 5 20 30 63"); else { for(int i = 0 ; i <= 4999 ; ++i) { ans[++c] = 6 * i + 2,ans[++c] = 6 * i + 3,ans[++c] = 6 * i + 4,ans[++c] = 6 * i + 6; } for(int i = 1 ; i <= N ; ++i) S = (S + ans[i]) % 6; if(S == 5) { ans[6] = 6 * 4999 + 4; } else if(S == 3) { ans[6] = 6 * 5000; } else if(S == 2){ ans[5] = 6 * 5000; } for(int i = 1 ; i <= N ; ++i) printf("%d%c",ans[i]," \n"[i == N]); } return 0; }
顯然第k個若是不加,比他小的都放進去都比它優code
因此咱們枚舉每一個k,每次把比它小的k都加進去,看看是否合法rem
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <ctime> #include <vector> //#define ivorysi #define MAXN 20005 #define eps 1e-8 #define mo 974711 #define pb push_back #define mp make_pair #define pii pair<int,int> #define fi first #define se second using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; const int64 MOD = 998244353; int N; int a[55],b[55]; int s[55],cnt,ans[55]; bool vis[55]; void Init() { scanf("%d",&N); for(int i = 1 ; i <= N ; ++i) scanf("%d",&a[i]); for(int i = 1 ; i <= N ; ++i) scanf("%d",&b[i]); } bool check(int x) { memset(vis,0,sizeof(vis)); vis[a[x]] = 1; for(int i = 1 ; i <= cnt ; ++i) { for(int j = s[i] ; j <= 50 ; ++j) { if(vis[j]) vis[j % s[i]] = 1; } } if(vis[b[x]]) return true; return false; } void Solve() { cnt = 0; for(int i = 50 ; i >= 1 ; --i) s[++cnt] = i; for(int i = 1 ; i <= N ; ++i) { if(!check(i)) {puts("-1");return;} } for(int i = 50 ; i >= 1 ; --i) { cnt = 0; for(int j = 50 ; j >= i ; --j) { if(ans[j]) s[++cnt] = j; } for(int j = i - 1 ; j >= 1 ; --j) s[++cnt] = j; for(int j = 1 ; j <= N ; ++j) { if(!check(j)) { ans[i] = 1; break; } } } int64 res = 0; for(int i = 50 ; i >= 1 ; --i) { if(ans[i]) res |= 1LL << i; } printf("%lld\n",res); } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Init(); Solve(); return 0; }
也就是求咱們最多轉多少圈,輸出乘上2×L就好get
咱們對於每一個位置求一個二元組\((x,y)\)
對於\(t\)處理成取模圈長以後的
\(x = 0\)表示從左邊來,下一次火車通過的時候是從右邊來,\(x = 1\)表示從左邊來,下一次火車通過的時候是從左邊來
\(y = 0\)表示從右邊來,下一次火車通過的時候從左邊來,\(y = 1\)表示從右邊來,下一次火車通過的時候從右邊來
顯然我\(x = 1\)從左邊進要多繞一圈,\(y = 1\)從右邊要多繞一圈string
\((1,1)\)的點能夠直接刪除,而後答案累加上1it
而後對於\((0,1)(0,1)(0,1)(0,0)(1,0)(1,0)\)這樣的形狀,顯然不能經過任何一個點走到另外一個點而不加任何額外的花費io
所用的圈數就是車站的個數 + 1
若是是(0,0)或者(0,1)做爲結尾,且後面沒有任何其他的車站時,就是圈數就是車站的個數class
實際上,咱們會有不少\((0,0)(0,1)\)和\((1,0)(0,0)\)和\((0,0)(0,0)\)這樣的形狀,咱們把他們兩兩搭配起來就好,圈數+1
要把\((0,0)\)和別的搭配完了再把\((0,0)\)兩兩搭配,這樣減小的圈最多,而且儘可能不配最右一個點
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define MAXN 300005 //#define ivorysi using namespace std; typedef long long int64; template<class T> void read(T &res) { res = 0;char c = getchar();T f = 1; while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) {out(x / 10);} putchar('0' + x % 10); } int N,tot,sta[MAXN],top; int64 L,x[MAXN],t[MAXN],ans; pii p[MAXN]; bool vis[MAXN]; void Solve() { read(N);read(L); for(int i = 1 ; i <= N ; ++i) read(x[i]); for(int i = 1 ; i <= N ; ++i) { read(t[i]); ans += t[i] / (2 * L); t[i] %= (2 * L); if(t[i] == 0) p[i] = mp(1,1); else p[i] = mp(t[i] > 2 * (L - x[i]),t[i] > 2 * x[i]); } for(int i = 1 ; i <= N ; ++i) { if(p[i].fi == 1 && p[i].se == 1) { vis[i] = 1; if(t[i] != 0) ++ans; } } top = 0; for(int i = 1 ; i <= N ; ++i) { if(!vis[i]) { if(p[i].fi == 1 && p[i].se == 0) { sta[++top] = i; } if(p[i].fi == 0 && p[i].se == 0) { if(top) { vis[i] = 1;vis[sta[top]] = 1; --top;++ans; } } } } top = 0; for(int i = 1; i < N ; ++i) { if(!vis[i]) { if(p[i].fi == 0 && p[i].se == 0) { sta[++top] = i; } if(p[i].fi == 0 && p[i].se == 1) { if(top) { vis[i] = 1;vis[sta[top]] = 1; --top;++ans; } } } } top = 0; for(int i = 1 ; i < N ; ++i) { if(!vis[i]) { if(p[i].fi == 0 && p[i].se == 0) { if(top) { vis[i] = 1;vis[sta[top]] = 1; --top;++ans; } else sta[++top] = i; } } } if(!vis[N] && p[N].fi == 0) { for(int i = 1 ; i < N - 1 ; ++i) { if(!vis[i] && p[i].se == 0) { vis[i] = 1;vis[N] = 1;++ans;break; } } } for(int i = 1 ; i <= N ; ++i) { if(!vis[i]) ++ans; } if(!vis[N]) { if(p[N].fi == 1 && p[N].se == 0) ++ans; } else ++ans; out(ans * 2 * L); } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }
這個E怎麼那麼水???
和上一道D題比起來簡直畫風不同……
分還同樣???
咱們顯然要貪心地把連着三個0變成一個0
若是開頭有兩個連續的1,咱們必定可使得最後的答案是1
若是開頭是一個0,咱們必定會把它弄走,並且它的合併必定是和它後面連續的兩個
那麼咱們就能夠記dp狀態爲\(dp[i][j][h][0/1]\)表示當前有j個0,開頭有h個1,有沒有出現形狀01
而後轉移就是每次加一個0
形狀01是010,合成一個0
兩個0和零個0都會變成1個0
一個0變成兩個0
每次加一個1
若是有兩個0會變成一個0
若是有一個0會變成01
若是沒有0就累加一個1
若是有01會累加一個1
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define space putchar(' ') #define enter putchar('\n') #define mp make_pair #define pb push_back //#define ivorysi #define MAXN 300005 using namespace std; typedef long long int64; typedef double db; template<class T> void read(T &res) { res = 0;char c = getchar();T f = 1; while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } const int MOD = 1000000007; int dp[MAXN][3][2][2],sum[MAXN],ans,N; char s[MAXN]; int inc(int a,int b) { return a + b >= MOD ? a + b - MOD : a + b; } int mul(int a,int b) { return 1LL * a * b % MOD; } void update(int &x,int y) { x = inc(x,y); } int fpow(int x,int c) { int res = 1,t = x; while(c) { if(c & 1) res = mul(res,t); t = mul(t,t); c >>= 1; } return res; } void Solve() { scanf("%s",s + 1); N = strlen(s + 1); for(int i = N ; i >= 1 ; --i) { sum[i] = sum[i + 1] + (s[i] == '?'); } dp[0][0][0][0] = 1; for(int i = 1 ; i <= N ; ++i) { int t = 0; for(int j = 0 ; j <= 2 ; ++j) { for(int h = 0 ; h <= 1 ; ++h) { for(int k = 0 ; k <= 1 ; ++k) { if(!dp[i - 1][j][h][k]) continue; if(s[i] == '0' || s[i] == '?') { if(k) update(dp[i][1][h][0],dp[i - 1][j][h][k]); else { if(j == 1) update(dp[i][2][h][0],dp[i - 1][j][h][k]); else update(dp[i][1][h][0],dp[i - 1][j][h][k]); } } if(s[i] == '1' || s[i] == '?') { if(k) { if(h) update(t,dp[i - 1][j][h][k]); else update(dp[i][0][h + 1][0],dp[i - 1][j][h][k]); } else { if(j == 1) update(dp[i][0][h][1],dp[i - 1][j][h][k]); if(j == 2) update(dp[i][1][h][0],dp[i - 1][j][h][k]); if(j == 0) { if(h) update(t,dp[i - 1][j][h][k]); else update(dp[i][0][h + 1][0],dp[i - 1][j][h][k]); } } } } } } update(ans,mul(t,fpow(2,sum[i + 1]))); } update(ans,inc(dp[N][0][1][1],dp[N][0][1][0])); out(ans);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }
一道結論題
很顯然的一點是,咱們至關於給每一個點上填上\(\pm 2^j\)
而後一個什麼樣的序列是合法的呢就是對於某一個位權\(i\)
若是咱們能夠經過改變全部\(2^i\)前面的符號,能夠達成全部\(0 \leq j \leq i\)使得他們的和等於\(1\),那麼這個集合就合法
能夠用概括法證,每次多一個新的\(2^i\)
這樣的話咱們能夠記一個\(dp[i][j]\)表示肯定了\(i\)位的值,全部數的和是\(1 + j * V\),咱們上一個加的最大的2的次冪是\(\frac{V}{2}\),由於\(2 * j\)是偶數因此總能夠變成\(0\)
答案就是\(dp[N][0]\)爲了處理負數能夠把第二維總體向前平移\(N\)
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define space putchar(' ') #define enter putchar('\n') #define mp make_pair #define MAXN 100005 //#define ivorysi using namespace std; typedef long long int64; template<class T> void read(T &res) { res = 0;char c = getchar();T f = 1; while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {putchar('-');x = -x;} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } const int MOD = 1000000007; int N; int dp[55][105],inv[55],invfac[55],fac[55]; int inc(int a,int b) { return a + b >= MOD ? a + b - MOD : a + b; } int mul(int a,int b) { return 1LL * a * b % MOD; } int C(int n,int m) { if(n < m) return 0; return mul(fac[n],mul(invfac[m],invfac[n - m])); } void update(int &x,int y) { x = inc(x,y); } void Solve() { read(N); inv[1] = 1; for(int i = 2 ; i <= N ; ++i) inv[i] = mul(inv[MOD % i],MOD - MOD / i); fac[0] = invfac[0] = 1; for(int i = 1 ; i <= N ; ++i) { fac[i] = mul(fac[i - 1],i); invfac[i] = mul(invfac[i - 1],inv[i]); } dp[1][N] = 1; dp[1][N - 1] = 1; for(int i = 1 ; i <= N ; ++i) { for(int j = -i ; j <= i ; ++j) { for(int k = max(1,abs(j)) ; k <= N ; ++k) { if(i + k > N) break; for(int h = 0 ; h <= k ; ++h) { if((j + 2 * h - k) % 2) continue; int val = (j + 2 * h - k) / 2; if(val < -N || val > N) continue; update(dp[i + k][val + N],mul(dp[i][j + N],mul(C(i + k,k),C(k,h)))); } } } } out(dp[N][N]);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }