【AtCoder】CODE FESTIVAL 2017 qual A

A - Snuke's favorite YAKINIKU

……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 MAXN 40005
#define eps 1e-12
//#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);
}
string s;
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    cin >> s;
    if(s.substr(0,4) == "YAKI") puts("Yes");
    else puts("No");
}

B - fLIP

枚舉N有幾個按了,M有幾列按了
對於一個點,行和列只有一個按了,那麼這個點是黑的spa

#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 40005
#define eps 1e-12
//#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,K;
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    read(N);read(M);read(K);
    for(int i = 0 ; i <= N ; ++i) {
        for(int j = 0 ; j <= M ; ++j) {
            int res = i * (M - j) + (N - i) * j;
            if(res == K) {puts("Yes");return 0;}
        }
    }
    puts("No");
    return 0;
}

C - Palindromic Matrix

從最外一圈開始推,咱們輸出須要幾個4個同樣的,2個同樣的,可能還有須要1個單個的
而後用每一個字母開始填4個同樣的,再填2個同樣的,再填1個單個的
若是沒填完,那麼就構造不出來code

#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 40005
#define eps 1e-12
//#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 num[30],cnt[10];
char s[105][105];

int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    read(N);read(M);
    for(int i = 1 ; i <= N ; ++i) {
        scanf("%s",s[i] + 1);
    }
    for(int i = 1 ; i <= N ; ++i) {
        for(int j = 1 ; j <= M ; ++j) {
            num[s[i][j] - 'a']++;
        }
    }
    int lr = 1,rr = N,lc = 1,rc = M;
    while(lr <= rr && lc <= rc) {
        int r = 0;
        if(lr != rr) r += 2;
        if(lc != rc) r += 2;
        if(!r) r = 1;
        cnt[r]++;
        for(int i = 1 ; i <= N ; ++i) {
            if(lr + i <= rr - i) {
                int r = 0;
                if(lr + i != rr - i) r += 2;
                if(lc != rc) r += 2;
                cnt[r]++;
            }
            else break;
        }
        for(int i = 1 ; i <= M ; ++i) {
            if(lc + i <= rc - i) {
                int r = 0;
                if(lc + i != rc - i) r += 2;
                if(rr != lr) r += 2;
                cnt[r]++;
            }
            else break;
        }
        ++lr;--rr;++lc;--rc;
    }
    for(int i = 0 ; i < 26 ; ++i) {
        while(cnt[4] && num[i] >= 4) {num[i] -= 4;--cnt[4];}
        while(cnt[2] && num[i] >= 2) {num[i] -= 2;--cnt[2];}
        while(cnt[1] && num[i] >= 1) {num[i] -= 1;--cnt[1];}
    }
    if(cnt[1] || cnt[2] || cnt[4]) puts("No");
    else puts("Yes");
    return 0;
}

D - Four Coloring

把圖旋轉45度後,再放一個每塊大小是d*d的方格,而後一行用RB間隔染色,下一行用GY間隔染色ip

#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 200005
#define eps 1e-12
//#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,d;
bool vis[1005][1005];
string s = "RBGY";
void Solve() {
    read(H);read(W);read(d);
    for(int i = 1 ; i <= H ; ++i) {
        for(int j = 1 ; j <= W ; ++j) {
            int t = 0;
            if((i + j - 1) / d & 1) t |= 2;
            if((i - j + 500 - 1) / d & 1) t |= 1;
            putchar(s[t]);
        }
        enter;
    }
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

E - Modern Painting

第一次確定是一個豎直的完整畫下來,或者一個水平的完整的畫下來
用豎直的舉例
就是枚舉一個區間\([L,R]\)都是完整的豎直畫下來,方案數是2的這個區間上下都有人的列個數次冪
而後算先後的那一塊方案數
確定是先橫着一刀,再豎着一刀,再橫着一刀
咱們把橫着的輪廓線畫出來,再把一邊的翻過去,發現是一個路徑計數,可是要求對稱軸必須有一個豎線
加入豎直有X我的,上有Y人,下有Z人,那麼若是直接走過去,方案數是
\(\binom{X + Y + Z}{X}\)
那咱們考慮一個序列,咱們就走\(X - 1\)個豎直的,橫向走到對稱軸後,緊接着就添加一個豎直的
方案數就是
\(\binom{X + Y + Z - 1}{X - 1}\)就是方案數ci

#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 200500
#define eps 1e-12
//#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 = 998244353;
int N,M;
char a[4][MAXN];
int fac[MAXN * 2],invfac[MAXN * 2],f[MAXN],b[MAXN],sum[4][MAXN],s[MAXN];
int pw[MAXN],ipw[MAXN],ans;
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 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 update(int &x,int y) {
    x = inc(x,y);
}
int C(int n,int m) {
    if(n < m) return 0;
    return mul(fac[n],mul(invfac[m],invfac[n - m]));
}
void CalcV() {
    memset(f,0,sizeof(f));
    memset(b,0,sizeof(b));
    memset(s,0,sizeof(s));
    int X = sum[0][N],Y,Z;
    for(int i = 0 ; i <= M ; ++i) {
        if(a[2][i + 1] == '0' && a[3][i + 1] == '0') continue;
        Y = sum[2][i],Z = sum[3][i];
        if(!X) {
            if(Y == 0 && Z == 0) f[i] = 1;
            else f[i] = 0;
        }
        else {
            f[i] = C(X + Y + Z - 1,X - 1);
        }
    }
    X = sum[1][N];
    for(int i = M + 1 ; i >= 1 ; --i) {
        if(a[2][i - 1] == '0' && a[3][i - 1] == '0') continue;
        Y = sum[2][M] - sum[2][i - 1],Z = sum[3][M] - sum[3][i - 1];
        if(!X) {
            if(Y == 0 && Z == 0) b[i] = 1;
            else b[i] = 0;
        }
        else {
            b[i] = C(X + Y + Z - 1,X - 1);
        }
    }
    for(int i = 1 ; i <= M ; ++i) s[i] = s[i - 1] + (a[2][i] == '1' && a[3][i] == '1');
    for(int i = 0 ; i <= M ; ++i) {
        f[i] = mul(f[i],ipw[s[i]]);
    }
    for(int i = M + 1 ; i >= 1 ; --i) {
        b[i] = inc(b[i + 1],mul(b[i],pw[s[i - 1]]));
    }
    for(int i = 1 ; i <= M ; ++i) {
        update(ans,mul(f[i - 1],b[i + 1]));
    }
}
void CalcH() {
    memset(f,0,sizeof(f));
    memset(b,0,sizeof(b));
    memset(s,0,sizeof(s));
    int X = sum[2][M],Y,Z;
    for(int i = 0 ; i <= N ; ++i) {
        if(a[0][i + 1] == '0' && a[1][i + 1] == '0') continue;
        Y = sum[0][i],Z = sum[1][i];
        if(!X) {
            if(Y == 0 && Z == 0) f[i] = 1;
            else f[i] = 0;
        }
        else {
            f[i] = C(X + Y + Z - 1,X - 1);
        }
    }
    X = sum[3][M];
    for(int i = N + 1 ; i >= 1 ; --i) {
        if(a[0][i - 1] == '0' && a[1][i - 1] == '0') continue;
        Y = sum[0][N] - sum[0][i - 1],Z = sum[1][N] - sum[1][i - 1];
        if(!X) {
            if(Y == 0 && Z == 0) b[i] = 1;
            else b[i] = 0;
        }
        else {
            b[i] = C(X + Y + Z - 1,X - 1);
        }
    }
    for(int i = 1 ; i <= N ; ++i) s[i] = s[i - 1] + (a[0][i] == '1' && a[1][i] == '1');
    for(int i = 0 ; i <= N ; ++i) {
        f[i] = mul(f[i],ipw[s[i]]);
    }
    for(int i = N + 1 ; i >= 1 ; --i) {
        b[i] = inc(b[i + 1],mul(b[i],pw[s[i - 1]]));
    }
    for(int i = 1 ; i <= N ; i++) {
        update(ans,mul(f[i - 1],b[i + 1]));
    }
}
void Solve() {
    read(N);read(M);
    for(int i = 0 ; i <= 3 ; ++i) scanf("%s",a[i] + 1);
    for(int i = 0 ; i <= 3 ; ++i) {
        int T = (i <= 1 ? N : M);
        for(int j = 1 ; j <= T ; ++j) {
            sum[i][j] = sum[i][j - 1] + (a[i][j] == '1');
        }
    }
    int T = 2 * (N + M) + 10;
    fac[0] = 1;
    for(int i = 1 ; i <= T ; ++i) {
        fac[i] = mul(fac[i - 1],i);
    }
    invfac[T] = fpow(fac[T],MOD - 2);
    for(int i = T - 1 ; i >= 0 ; --i) {
        invfac[i] = mul(invfac[i + 1],i + 1);
    }
    pw[0] = 1;
    T = max(N,M);
    for(int i = 1 ; i <= T ; ++i) {
        pw[i] = mul(pw[i - 1],2);
    }
    ipw[0] = 1;
    for(int i = 1 ; i <= T ; ++i) {
        ipw[i] = mul(ipw[i - 1],(MOD + 1) / 2);
    }
    CalcV();
    CalcH();
    if(!sum[0][N] && !sum[1][N] && !sum[2][M] && !sum[3][M]) ans = 1;
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

F - Squeezing Slimes

對於一個單個的區間來講,咱們能夠用
若是\(a = 2^{k}\)能用k次合成
若是\(2^{k} < a < 2^{k + 1}\),能用k+1次合成
咱們記錄一下每一個區間最左的元素被選了幾回,最右的區間選了幾回
若是是\(a = 2^{k}\),那麼兩邊就選了k次
另外一種有兩個狀況,左邊k次右邊k+1次,左邊k+1次右邊k次
每一個相鄰的地方能夠減小\(min(r_{i},l_{i + 1})\)
用一個dp實現就行get

#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-12
//#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;
int a[MAXN],cnt[MAXN],val[MAXN][2];
int64 dp[MAXN][2];
void Solve() {
    read(N);
    for(int i = 1 ; i <= N ; ++i) {
        read(a[i]);
        int k = -1,x = a[i];
        while(x) {++k;x >>= 1;}
        cnt[i] = k;
    }
    for(int i = 1 ; i <= N ; ++i) {
        dp[i][0] = 1e16;dp[i][1] = 1e16;
        int s = cnt[i],t = cnt[i];
        if(a[i] != (1 << cnt[i])) ++t;
        val[i][0] = s;val[i][1] = t;
        for(int j = 0 ; j <= 1 ; ++j) {
            dp[i][0] = min(dp[i][0],dp[i - 1][j] + t - min(t,val[i - 1][j]));
            dp[i][1] = min(dp[i][1],dp[i - 1][j] + t - min(s,val[i - 1][j]));
        }
    }
    out(min(dp[N][0],dp[N][1]));enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}
相關文章
相關標籤/搜索