用每一個字母模擬一下就行c++
#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 eps 1e-10 #define MAXN 200005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); 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); } char s[105]; bool vis[30],a[105],b[105]; int N; void Solve() { scanf("%s",s + 1); N = strlen(s + 1); for(int i = 1 ; i <= N ; ++i) vis[s[i] - 'a'] = 1; int ans = N; for(int t = 0 ; t < 26 ; ++t) { if(vis[t]) { int cnt = 0; memset(a,0,sizeof(a)); for(int i = 1 ; i <= N ; ++i) a[i] = (s[i] == 'a' + t); while(1) { bool f = 1; for(int i = 1 ; i <= N - cnt; ++i) { f = f & a[i]; } if(f) break; memset(b,0,sizeof(b)); for(int i = 1 ; i <= N - cnt - 1; ++i) { b[i] = a[i] || a[i + 1]; } ++cnt; memcpy(a,b,sizeof(a)); } ans = min(ans,cnt); } } out(ans);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }
若是最大值和最小值相等,要麼最大值等於N - 1,不然就是最大值乘2小於N
若是相差1,最小值的個數是t
那麼要知足最大值\(t + 1\leq A \leq t + (N - t) / 2\)函數
#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 eps 1e-10 #define MAXN 200005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); 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,a[MAXN]; int tot; void Solve() { read(N); int maxx = 0,minn = N; for(int i = 1 ; i <= N ; ++i) { read(a[i]);maxx = max(a[i],maxx);minn = min(a[i],minn); } if(maxx - minn > 1) {puts("No");return;} if(maxx == minn) { if(maxx == N - 1) puts("Yes"); else if(maxx * 2 <= N) puts("Yes"); else puts("No"); return; } for(int i = 1 ; i <= N ; ++i) { if(a[i] == minn) ++tot; } if(maxx <= tot) {puts("No");return;} if(N - tot >= 2 * (maxx - tot)) { puts("Yes");return; } puts("No"); } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }
若是\(H\)是\(h\)的倍數而且\(W\)是\(w\)的倍數,那麼無解spa
不然認爲\(H\)不是\(h\)的倍數,以0開始標號,每一個\(h\)的倍數的行都填成正數\(1000(h - 1) - 1\),其餘行都填成-1000code
#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 eps 1e-10 #define MAXN 200005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); 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 H,W,h,w; void Solve() { read(H);read(W);read(h);read(w); if(H % h != 0) { puts("Yes"); for(int i = 0 ; i < H ; ++i) { for(int j = 0 ; j < W ; ++j) { if(i % h == 0) {out(1000 * (h - 1) - 1);} else out(-1000); space; } enter; } } else if(W % w != 0) { puts("Yes"); for(int i = 0 ; i < H ; ++i) { for(int j = 0 ; j < W ; ++j) { if(j % w == 0) {out(1000 * (w - 1) - 1);} else out(-1000); space; } enter; } } else { puts("No"); } } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }
至關於最後多了一個位置,是a的異或和,而後每次至關於交換最後多的這個位置上的數和選中的數get
而後咱們按照權值建點,a和b對應位置的數值連邊,起點爲a的異或和,終點爲多出來的數,這兩點中間連一條邊,咱們須要每一個聯通塊有歐拉回路,因此統計奇數點的個數,除2是附加邊的個數,而後走完一個聯通塊到下一個聯通塊中不用走回來,因此是多連一條便可it
#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 100005 #define eps 1e-10 //#define ivorysi using namespace std; typedef long long int64; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); 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 a[MAXN],b[MAXN],N,val[MAXN],tot,st,ed,deg[MAXN],cnt; int fa[MAXN]; map<int,int> zz; int getfa(int u) { return fa[u] == u ? u : fa[u] = getfa(fa[u]); } void Init() { read(N); for(int i = 1 ; i <= N ; ++i) {read(a[i]);a[N + 1] ^= a[i];} for(int i = 1 ; i <= N ; ++i) read(b[i]); for(int i = 1 ; i <= N + 1 ; ++i) { zz[a[i]]++; } } void Solve() { for(int i = 1 ; i <= N ; ++i) { if(zz[b[i]] == 0) { puts("-1");return; } zz[b[i]] -= 1; } st = a[N + 1]; for(int i = 1 ; i <= N + 1; ++i) { val[++tot] = a[i]; if(zz[a[i]]) ed = a[i]; } sort(val + 1,val + tot + 1); tot = unique(val + 1,val + tot + 1) - val - 1; st = lower_bound(val + 1,val + tot + 1,st) - val; ed = lower_bound(val + 1,val + tot + 1,ed) - val; for(int i = 1 ; i <= tot ; ++i) { fa[i] = i; } for(int i = 1 ; i <= N ; ++i) { if(a[i] != b[i]) { int p = lower_bound(val + 1,val + tot + 1,a[i]) - val; int q = lower_bound(val + 1,val + tot + 1,b[i]) - val; ++deg[p];++deg[q]; fa[getfa(p)] = getfa(q); ++cnt; } } ++deg[st];++deg[ed]; int p = 0; for(int i = 1 ; i <= tot ; ++i) { if(deg[i] & 1) ++p; } cnt += p / 2; for(int i = 1 ; i <= tot ; ++i) { if(deg[i]) { if(getfa(i) != getfa(st)) { fa[getfa(i)] = getfa(st); cnt++; } } } out(cnt);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Init(); Solve(); }
若是一個點集S在t次操做後不會被消滅,有四種狀況class
\(x_{t} \in S,y_{t} \in S\)那麼必定會被消滅date
若是\(x_{t} \in S,y_{t} \notin S\)那麼前\(t - 1\)次\(S \cup y_{t}\)必須得存在map
若是\(x_{t} \notin S,y_{t} \in S\)那麼前\(t - 1\)次\(S\cup x_{t}\)必須得存在統計
若是\(x_{t} \notin S,y_{t} \notin S\)那麼前\(t - 1\)次\(S\)必須存在
因此對於一個點\(v\)是否最後能夠存在,那麼能夠用\({v}\)日後倒推
對於一個點對必須存在
\(v\)不會被吃掉
\(u\)不會被吃掉
操做兩個點對獲得的集合沒有相同的點
#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 eps 1e-10 #define MAXN 100005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); 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,M; int x[MAXN],y[MAXN]; bool S[405][405],eat[405]; void Solve() { read(N);read(M); for(int i = 1 ; i <= M ; ++i) { read(x[i]);read(y[i]); } for(int i = 1 ; i <= N ; ++i) { S[i][i] = 1; for(int j = M ; j >= 1 ; --j) { if(S[i][x[j]] && S[i][y[j]]) {eat[i] = 1;break;} else if(S[i][x[j]] && !S[i][y[j]]) S[i][y[j]] = 1; else if(!S[i][x[j]] && S[i][y[j]]) S[i][x[j]] = 1; } } int ans = 0; for(int i = 1 ; i <= N ; ++i) { for(int j = i + 1 ; j <= N ; ++j) { if(!eat[i] && !eat[j]) { bool flag = 1; for(int h = 1 ; h <= N ; ++h) { if(S[i][h] && S[j][h]) {flag = 0;break;} } ans += flag; } } } out(ans);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }
用\(dp[S]\)表示給\(S\)之間的點加邊使得1和2的sg函數相同
咱們能夠把\(S\)劃分紅兩個點集\(T\)和\(U\)保證這兩個點集要麼都有12,要麼12一個都沒有
\(U\)之間沒有邊
\(U\)到\(T\)的邊隨意
\(T\)到\(U\)必須有一條邊
而後\(T\)之間的邊連邊方式是\(dp[T]\)
答案是\(2^{M} - dp[2^{N} - 1]\)
#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 eps 1e-10 #define MAXN 100005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); 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 N,M; int c[16],dp[(1 << 15) + 5],cnt[(1 << 15) + 5],pw[100005]; 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 lowbit(int x) { return x & (-x); } void update(int &x,int y) { x = inc(x,y); } void Solve() { read(N);read(M); pw[0] = 1; for(int i = 1 ; i <= M ; ++i) pw[i] = mul(pw[i - 1],2); for(int i = 1 ; i < (1 << N) ; ++i) cnt[i] = cnt[i - lowbit(i)] + 1; int x,y; for(int i = 1 ; i <= M ; ++i) { read(x);read(y); c[x] |= 1 << y - 1; } dp[0] = 1; for(int S = 1 ; S < (1 << N) ; ++S) { if((S & 3) != 3 && (S & 3) != 0) continue; for(int T = S; T ; T = (T - 1) & S) { if((T & 3) != 3 && (T & 3) != 0) continue; int d = 1; for(int i = 1 ; i <= N ; ++i) { if(T & (1 << i - 1)) d = mul(d,pw[cnt[c[i] & (S ^ T)]]); if((S ^ T) & (1 << i - 1)) d = mul(d,pw[cnt[c[i] & T]] - 1); } d = mul(d,dp[S ^ T]); update(dp[S],d); } } out(inc(pw[M],MOD - dp[(1 << N) - 1]));enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }