【AtCoder】diverta 2019 Programming Contest

diverta 2019 Programming Contest

由於評測機的緣故……它unrated了。。c++

A - Consecutive Integers

#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 1005
//#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,K;
void Solve() {
    read(N);read(K);
    out(N - K + 1);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}

B - RGB Boxes

……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 eps 1e-10
#define MAXN 1005
//#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 R,G,B,N;
void Solve() {
    read(R);read(G);read(B);read(N);
    int cnt = 0;
    for(int i = 0 ; i <= N / R ; ++i) {
        int t = N - i * R;
        for(int j = 0 ; j <= t / G ; ++j) {
            int h = N - i * R - j * G;
            if(h % B == 0) ++cnt;
        }
    }
    out(cnt);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}

C - AB Substrings

丟人選手交了6遍,罰時+++++code

就是記錄BA,和只有前面有B,只有後面有Arem

*A\BA\B*get

這樣三個拼兩個string

而後若是隻剩*A和B*就都配起來it

不然就看一開始是否存在*A和B*,把BA都配起來io

#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 1005
//#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;
char s[MAXN];
int a[3];
void Solve() {
    read(N);
    int ans = 0;
    for(int i = 1 ; i <= N ; ++i) {
        scanf("%s",s + 1);
        int l = strlen(s + 1);
        for(int j = 1 ; j < l ; ++j) {
            if(s[j] == 'A' && s[j + 1] == 'B') ++ans;
        }
        if(s[1] == 'B' && s[l] == 'A') ++a[2];
        else if(s[1] == 'B') ++a[1];
        else if(s[l] == 'A') ++a[0];
    }
    int t = min(a[2],min(a[0],a[1]));
    ans += t * 2;
    a[2] -= t;a[0] -= t;a[1] -= t;
    ans += min(a[0],a[1]);
    if(a[2]) {
        if(t) ans += a[2];
        else if(max(a[0],a[1])) ans += a[2];
        else ans += a[2] - 1;
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}

D - DivRem Number

\(\lfloor \frac{N}{i} \rfloor\)只有\(\sqrt{N}\)種取值,枚舉出來求出m而後看m在不在對應區間便可class

#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 1005
//#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);
}
int64 N;
void Solve() {
    read(N);
    int64 ans = 0;
    for(int64 i = 1 ; i <= N ; ++i) {
        int64 r = N / (N / i);
        int64 t = N / i;
        if(N % t == 0) {
            int64 k = N / t - 1;
            if(k >= i && k <= r) ans += k;
        }
        i = r;
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}

E - XOR Partitioning

若是一段不爲0的話,那麼這些位置的前綴和確定是a0a0a0a0,這樣的話就\(dp[i]\)表示這個數爲結尾,而後找前一段和它相同的\(dp[j]\)\(s[i] == s[j]\),轉移乘上中間全部的0的個數,能夠經過拆成前綴和分項維護test

最後計算每一段爲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 eps 1e-10
#define MAXN 500005
//#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 MOD = 1000000007;
int N;
int A[MAXN],sum[MAXN],s[MAXN];
int pre[2][(1 << 20) + 5],dp[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() {
    read(N);
    for(int i = 1 ; i <= N ; ++i) {
        read(A[i]);
        s[i] = s[i - 1] ^ A[i];
        sum[i] = sum[i - 1] + (s[i] == 0);
    }
    int all = 0;
    for(int i = 1 ; i <= N ; ++i) {
        if(s[i] != 0) {
            dp[i] = mul(pre[0][s[i]],sum[i - 1]) + 1;
            update(dp[i],MOD - pre[1][s[i]]);
        }
        else dp[i] = inc(all,MOD - pre[0][0]);
        update(pre[0][s[i]],dp[i]);
        update(pre[1][s[i]],mul(dp[i],sum[i]));
        update(all,dp[i]);
    }
    if(s[N] == 0) {
        update(dp[N],fpow(2,sum[N] - 1));
    }
    out(dp[N] % MOD);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}

F - Edge Ordering

假如樹邊的大小固定了,那麼非樹邊的必須大於樹邊中最大的那條

若是咱們認爲非樹邊是白球,樹邊是黑球,就是先填被最大的邊控制的白球,而後填最大的黑球,填被次大的邊控制的白球,填次大的黑球……

這個序列應該是來了白球能夠在任意位置,來了黑球必須放到序列最前面

那麼怎麼統計價值和呢,記錄序列長度n,序列種類c,黑球個數b,和價值和s

若是來了一個黑球

\((n,c,b,s)\rightarrow (n + 1,c,b + 1,s + (b +1)c)\)

若是來了一個白球

\((n,c,b,s)\rightarrow(n + 1,c(n +1),b,s(n + 2))\)

爲啥是\(n + 2\)呢。。由於若是把那個白球從前移到後,會發現\(s\)被加了\((n +1)\)次,餘下的零頭是原來全部黑球的座標和

而後若是來了k個白球

\((n,c,b,s) \rightarrow (n + k,c(n + 1)(n + 2)\cdots(n + k),b,s(n + 2)(n + 3)\cdots(n + k + 1))\)

那麼又回到剛開始的假設了,如何肯定樹邊的大小呢

能夠用dp從大到小分配每一個樹邊

設S表示集合爲S的樹邊已經被固定了

每次新加一條樹邊控制的非樹邊,是加以後非樹邊的兩個點連通的非樹邊的個數,減掉加以前非樹邊兩個點連通非樹邊的個數

這個只要對於每一種邊集連起來,而後搜出每一個聯通塊,計算聯通塊之間邊相連的數目再減去聯通塊裏樹邊的數目便可

聯通塊裏邊的數目能夠很容易經過dp算出

#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 500005
//#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 a[205],b[205],fac[205],invfac[205];
int col[(1 << 20) + 5][21],num[(1 << 20) + 5],c[21];
int to[21],e[(1 << 20) + 5],cnt[(1 << 20) + 5];
int dp[(1 << 19) + 5],sum[(1 << 19) + 5],len[(1 << 19) + 5];
int fa[21];
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 lowbit(int x) {
    return x & (-x);
}
int getfa(int x) {
    return fa[x] == x ? x : fa[x] = getfa(fa[x]);
}
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() {
    read(N);read(M);
    for(int i = 1 ; i <= M ; ++i) {
        read(a[i]);read(b[i]);
        to[a[i]] |= (1 << b[i] - 1);
        to[b[i]] |= (1 << a[i] - 1);
    }
    fac[0] = 1;
    for(int i = 1 ; i <= 200 ; ++i) fac[i] = mul(fac[i - 1],i);
    invfac[200] = fpow(fac[200],MOD - 2);
    for(int i = 199 ; i >= 0 ; --i) invfac[i] = mul(invfac[i + 1],i + 1);
    for(int S = 0 ; S < (1 << N) ; ++S) {
        if(S != 0) cnt[S] = cnt[S - lowbit(S)] + 1;
        for(int j = 1 ; j <= N ; ++j) {
            if(S >> (j - 1) & 1) {
                int T = to[j] & S;
                e[S] = e[S ^ (1 << j - 1)] + cnt[T];break;
            }
        }
    }
    for(int S = 0 ; S < (1 << N - 1) ; ++S) {
        for(int i = 1 ; i <= N ; ++i) fa[i] = i;
        for(int i = 1 ; i < N ; ++i) {
            if(S >> (i - 1) & 1) {
                fa[getfa(a[i])] = getfa(b[i]);
            }
        }
        int tot = 0;
        for(int i = 1 ; i <= N ; ++i) {
            if(fa[i] == i) {col[S][i] = ++tot;c[tot] = 0;}
        }
        for(int i = 1 ; i <= N ; ++i) {
            col[S][i] = col[S][getfa(i)];
            int t = col[S][i];
            c[t] |= (1 << i - 1);
        }
        for(int i = 1 ; i <= tot ; ++i) {
            num[S] += e[c[i]] - (cnt[c[i]] - 1);
        }
    }
    dp[0] = 1;sum[0] = 0;
    int ALL = (1 << N - 1) - 1;
    for(int S = 0 ; S < (1 << N - 1) ; ++S) {
        for(int j = 1 ; j < N ; ++j) {
            if(!(S >> (j - 1) & 1)) {
                int a = num[ALL ^ S] - num[ALL ^ S ^ (1 << j - 1)];
                int T = S ^ (1 << j - 1);
                int n = len[S];
                len[T] = n + a + 1;
                int c = mul(dp[S],mul(fac[n + a],invfac[n]));
                int s = mul(sum[S],mul(fac[n + a + 1],invfac[n + 1]));
                update(s,mul(c,cnt[S] + 1));
                update(dp[T],c);update(sum[T],s);
            }
        }
    }
    out(sum[(1 << N - 1) - 1]);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}
相關文章
相關標籤/搜索