【AtCoder】CODE FESTIVAL 2017 qual C

A - Can you get AC?

Noc++

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long int64;
char s[15];
int main() {
    scanf("%s",s + 1);
    int l = strlen(s + 1);
    for(int i = 1 ; i < l ; ++i) {
        if(s[i] == 'A' && s[i + 1] == 'C') {
            puts("Yes");return 0;
        }
    }
    puts("No");return 0;
}

B - Similar Arrays

dp[i][1/0]表示到第i個數乘積是奇數或偶數優化

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define enter putchar('\n')
#define space putchar(' ')
//#define ivorysi
using namespace std;
typedef long long int64;
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[15];
int64 dp[15][2];
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    read(N);
    for(int i = 1 ; i <= N ; ++i) read(A[i]);
    dp[0][1] = 1;
    for(int i = 1 ; i <= N ; ++i) {
        for(int j = -1 ; j <= 1 ; ++j) {
            if((A[i] + j) & 1) {
                dp[i][1] += dp[i - 1][1];
                dp[i][0] += dp[i - 1][0];
            }
            else {
                dp[i][0] += dp[i - 1][0] + dp[i - 1][1];
            }
        }
    }
    out(dp[N][0]);enter;
}

C - Inserting 'x'

從左右兩邊各一個指針,若是匹配就往裏走
若是不匹配且某一個爲x,則把爲x的那個往裏走
若是不是則沒法變成迴文串spa

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define enter putchar('\n')
#define space putchar(' ')
//#define ivorysi
using namespace std;
typedef long long int64;
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[100005];
int N;
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    scanf("%s",s + 1);
    N = strlen(s + 1);
    int p = 1,q = N;
    int ans = 0;
    while(p < q) {
        if(s[p] == s[q]) {++p;--q;}
        else {
            if(s[p] == 'x') {++p;++ans;}
            else if(s[q] == 'x') {--q;++ans;}
            else {puts("-1");return 0;}
        }
    }
    out(ans);enter;return 0;
}

D - Yet Another Palindrome Partitioning

記錄一下一個位置前綴和奇偶性,壓成一個27bit的數s
這個位置能從前面和s相同的位置和s改了一位的位置轉移過來
不一樣的s只有n個,拿map記一下就好指針

#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-10
//#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[MAXN];
int sum[MAXN],N;
int dp[MAXN];
map<int,int> zz;
void Init() {
    scanf("%s",s + 1);
    N = strlen(s + 1);
    for(int i = 1 ; i <= N ; ++i) {
        sum[i] = sum[i - 1];
        sum[i] ^= (1 << s[i] - 'a');
    }
}
void Solve() {
    zz[0] = 0;
    for(int i = 1 ; i <= N ; ++i) {
        dp[i] = i;
        if(zz.count(sum[i])) dp[i] = min(dp[i],zz[sum[i]] + 1);
        for(int j = 0 ; j < 26 ; ++j) {
            if(zz.count(sum[i] ^ (1 << j))) dp[i] = min(dp[i],zz[sum[i] ^ (1 << j)] + 1);
        }
        if(!zz.count(sum[i])) zz[sum[i]] = dp[i];
        else zz[sum[i]] = min(zz[sum[i]],dp[i]);
    }
    out(dp[N]);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Init();
    Solve();
}

E - Cubes

注意讀題,有句話是ABC兩兩互質
那麼一共通過的方塊數是
A + B + C - 2
認爲有一位通過某個整數則通過了一個方塊
那麼其實能夠這麼認爲
A + B + C - gcd(A,B) - gcd(B,C) - gcd(B,A) + gcd(A,B,C)
因爲A,B,C互質,那麼每次路徑上的相鄰兩個方塊確定有一個面重合
若是去掉那個方塊的限制,那麼答案是
\((2D + 1)^3 + (A + B + C - 3) \cdot (2D + 1)^2\)
就是以路徑上一個點爲中心上下左右各\(D\)個點
每次增量是一個面
那麼如何計算交呢,咱們須要三維每一維分別取出前不足D的點和後不足D的點
能夠用分數記錄一下這些點,個數只有\(O(D)\)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 enter putchar('\n')
#define space putchar(' ')
//#define ivorysi
using namespace std;
typedef long long int64;
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;
int64 t[3],D;
vector<pair<int64,int64> > v;
int 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 mul3(int a,int b,int c) {
    return mul(mul(a,b),c);
}
void update(int &x,int y) {
    x = inc(x,y);
}
void Solve() {
    for(int i = 0 ; i < 3 ; ++i) read(t[i]);read(D);
    for(int64 i = 0 ; i <= D ; ++i) {
    for(int j = 0 ; j < 3 ; ++j) {
        if(i && i < t[j]) v.pb(mp(i,t[j]));
        if(t[j] - 1 - i > 0) v.pb(mp(t[j] - i - 1,t[j]));
    }
    }
    sort(v.begin(),v.end(),[](pair<int64,int64> a,pair<int64,int64> b){return a.fi * b.se < b.fi * a.se;});
    v.erase(unique(v.begin(),v.end()),v.end());
    int64 a[3] = {0,0,0};
    update(ans,mul3(min(t[0],D + 1),min(t[1],D + 1),min(t[2],D + 1)));
    for(auto k : v) {
    int64 b[3] = {a[0],a[1],a[2]};
    for(int j = 0 ; j < 3 ; ++j) {
        a[j] = t[j] * k.fi / k.se;
    }
    int64 d[3];
    for(int j = 0 ; j < 3 ; ++j) {
        d[j] = min(b[j] + D,t[j] - 1) - max(b[j] - D,0LL) + 1;
    }
    for(int j = 0 ; j < 3 ; ++j) {
        if(a[j] + D < t[j]) {
        int t = a[j] - b[j];
        for(int h = 0 ; h < 3 ; ++h) {
            if(h != j) t = mul(t,d[h]);
        }
        update(ans,t);
        }
    }
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

F - Three Gluttons

作atc總以爲本身是個智障,早點退役保平安
條件我都沒分析出來。。。= =three

就是認爲咱們把這個分紅三個數字不一樣的序列,每一個長度是\(N / 3\)
\(t\)次吃要知足\(a_{1},a_{2},....a_{i_t},b_{1},b_{2},...b_{j_t}\)
\(a_{i_t}\)\(b_{j_t}\)只出現了一次
這樣保證了兩個序列裏不會選重
而後第三個序列假如吃的是\(x_{1},x_{2}...x_{\frac{N}{3}}\)
我要知足\(x_{t}\)
\(a_{1},a_{2},....a_{i_t},b_{1},b_{2},...b_{j_t}\)沒有出現過get

而後呢,若是咱們找出一個知足條件的吃的三個序列,你會發現,這樣第三種序列填數的方案,和我選了什麼並無關係!!!!!
而後設\(dp[i][j]\)表示考慮到第i個,已經填在c序列裏的有j個
i沒增長1,能填的數會多兩個,直接dp就行it

而後就是怎麼求三個合法序列了
從後往前推,發現\(x_{t}\)不能填的數就是\(a_{1},a_{2},....a_{i_t},b_{1},b_{2},...b_{j_t}\)出現過的數,a,b,c的後\(n - t\)
前綴和優化一下能夠作到\(N^{3}\)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 MAXN 20000005
#define eps 1e-10
//#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 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;
}
int dp[150][405],N,f[150][405][405],w,ans;
int a[405],b[405],fac[405],invfac[405];
bool visa[405],visb[405],vis[405][405];
int cnt[405][405],g[405];
int A(int n,int m) {
    if(n < m) return 0;
    return mul(fac[n],invfac[n - m]);
}
void Solve() {
    read(N);
    for(int i = 1 ; i <= N ; ++i) read(a[i]);
    for(int i = 1 ; i <= N ; ++i) read(b[i]);
    fac[0] = 1;
    for(int i = 1 ; i <= N ; ++i) fac[i] = mul(fac[i - 1],i);
    invfac[N] = fpow(fac[N],MOD - 2);
    for(int i = N - 1 ; i >= 0 ; --i) invfac[i] = mul(invfac[i + 1],i + 1);
    dp[1][2] = 1;
    for(int i = 1 ; i < N / 3 ; ++i) {
        for(int j = 0 ; j <= i * 2 ; ++j) {
            for(int h = 0 ; h <= j; ++h) {
                update(dp[i + 1][j + 2 - h],mul(A(j,h),dp[i][j]));
            }
        }
    }
    for(int j = 0 ; j <= (N / 3) * 2 ; ++j) update(w,mul(dp[N / 3][j],fac[j]));
    for(int i = 1 ; i <= N ; ++i) {
        visa[a[i]] = 1;
        cnt[i][0] = i;
        memset(visb,0,sizeof(visb));
        for(int j = 1 ; j <= N ; ++j) {
            visb[b[j]] = 1;
            cnt[i][j] = cnt[i][j - 1];
            if(!visa[b[j]]) cnt[i][j]++;
            if(!visa[b[j]] && !visb[a[i]]) vis[i][j] = 1;
        }
    }
    for(int t = N / 3 ; t >= 1 ; --t) {
        memset(g,0,sizeof(g));

        for(int i = N ; i >= 1 ; --i) {
            int s = (t == N / 3);
            for(int j = N ; j >= 1 ; --j) {
                if(vis[i][j]) f[t][i][j] = mul(s,N - 3 * (N / 3 - t) - cnt[i][j]);
                update(s,g[j]);
                update(g[j],f[t + 1][i][j]);
            }
        }
    }
    for(int i = 1 ; i <= N ; ++i) {
        for(int j = 1 ; j <= N ; ++j) {
            update(ans,f[1][i][j]);
        }
    }
    ans = mul(ans,w);
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}
相關文章
相關標籤/搜索