1. 程式人生 > 其它 >普及組膜你賽

普及組膜你賽

久違了rank1

T1(李時珍的面板衣)

就是找個規律....手推幾個就行2 ^ n - 1 + 1

``

點選檢視程式碼
#include <bits/stdc++.h>
#define Re register int
#define LL long long
#define ki cout << endl; 
#define WMX aiaiaiai~~

using namespace std;
namespace kiritokazuto{
	template <typename T> inline void in(T &x) {
		int f = 0; x = 0; char c = getchar();
		while(c < '0' || c > '9')f |= c == '-', c = getchar();
		while(c >= '0' && c <= '9')x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
		x = f ? -x : x;
	}
	template <typename T> inline void ot(T x) {
		if(x < 0)putchar('-'), x = -x;
		if(x > 9)ot(x / 10);putchar(x % 10 | '0');
	}
}
using namespace kiritokazuto;

inline LL qpow(LL a, LL b, LL p) {
    LL ans = 1;
    while(b) {
        if(b & 1) ans = ans * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return ans;
}
LL n;
signed main () {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    freopen("lsz.in", "r", stdin);
    freopen("lsz.out", "w", stdout);
    in(n);
    LL tp = qpow(2, n - 1, n);
    tp = (tp + 1) % n;
    ot(tp);
}

T2

就是個tri樹的板子,把所有的都拆出來就行
坑點就是把記憶體算好,我全開LL就MLE了,爆了10tps .....我的AK啊!!!!!!!!!

點選檢視程式碼
#include <bits/stdc++.h>
#define Re register int
#define LL int
#define ki cout << endl; 
#define WMX aiaiaiai~~

using namespace std;
namespace kiritokazuto{
	template <typename T> inline void in(T &x) {
		int f = 0; x = 0; char c = getchar();
		while(c < '0' || c > '9')f |= c == '-', c = getchar();
		while(c >= '0' && c <= '9')x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
		x = f ? -x : x;
	}
	template <typename T> inline void ot(T x) {
		if(x < 0)putchar('-'), x = -x;
		if(x > 9)ot(x / 10);putchar(x % 10 | '0');
	}
}
using namespace kiritokazuto;

const int maxn = 1e6 + 28900;
//得,我又回憶起當時在角樓老姚講題時我們被tri樹支配的恐懼
//當時好像還覺得tri是個遙不可及的東西來著
//時節不拘,歲月如流啊,唉
LL n, m;
char s[25];
LL tim;
LL cnt[maxn][30];
LL tri[maxn][30];
LL nex[maxn][30];
//struct Tri {
    // LL cnt[maxn][27];
    // LL tri[maxn][27];
    // LL nex[maxn][27];
    // inline void init() {
    //     memset(cnt, 0, sizeof(cnt));
    //     memset(tri, 0, sizeof(tri));
    //     memset(nex, 0, sizeof(nex));
    // }//雖然很帥,但是太慢

    inline void insert(LL st, LL ed, LL id) {
        LL rt = 0;
        for(Re i = st; i < ed; i ++) {//不是找前後綴,所以每一個都得存上
        //淦,不能去等
            LL tmp = s[i] - 'a';
            if(tri[rt][tmp]) {
                if(nex[rt][tmp] != id) {nex[rt][tmp] = id; cnt[rt][tmp]++;}
            } else {
                tri[rt][tmp] = ++tim;//這裡是++tim, 不是tim++!!!!!!!!!!!!!!!!!!!重了啊
                nex[rt][tmp] = id;
                cnt[rt][tmp]++;
            }
            rt = tri[rt][tmp];
        } 
    }
    inline LL query(LL lens) {
        LL rt = 0;
        LL ans = 0;
        for(Re i = 0; i < lens; i ++) {
            LL to = s[i] - 'a';
            if(!tri[rt][to])return 0;
            ans = cnt[rt][to];
            rt = tri[rt][to];
        }
        return ans;
    }
//}wmx;
signed main () {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    freopen("mdz.in", "r", stdin);
    freopen("mdz.out", "w", stdout);
    in(n);
    for(Re i = 1; i <= n; i ++) {//向字串低頭,從0存
        cin >> s;
        LL len = strlen(s);
        for(Re j = 0; j < len; j ++) {
            insert(j, len, i);
        }
    }
    in(m);
    for(Re i = 1; i <= m; i ++) {
        cin >> s;
        cout << query(strlen(s)) << endl;
    }
}

樣例輸入

20
ad
ae
af
ag
ah
ai
aj
ak
al
ads
add
ade
adf
adg
adh
adi
adj
adk
adl
aes
5
b
a
d
ad
s

樣例輸出

0
20
11
11
2

T3

就是一個奇奇怪怪的記憶化搜尋同時也是sb狀壓dp
以後再遞迴的時候千萬要記得不要memset太多
又T了30分,AK無望
同時hash是為了記憶化搜尋,我把之後剩的數狀態相同的都給他對映加狀壓了
還有就是處理負數的時候不要while了!!!先模再加就行

點選檢視程式碼
#include <bits/stdc++.h>
#define Re register int
#define LL long long
#define ki cout << endl; 
#define WMX aiaiaiai~~

using namespace std;
namespace kiritokazuto{
	template <typename T> inline void in(T &x) {
		int f = 0; x = 0; char c = getchar();
		while(c < '0' || c > '9')f |= c == '-', c = getchar();
		while(c >= '0' && c <= '9')x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
		x = f ? -x : x;
	}
	template <typename T> inline void ot(T x) {
		if(x < 0)putchar('-'), x = -x;
		if(x > 9)ot(x / 10);putchar(x % 10 | '0');
	}
}
using namespace kiritokazuto;

const int maxn = 1e6 + 10;
const LL Mod = 1234567891;
//得,開始模第三題,AK的希望
// ( 不 ) 為給定整數M的倍數!!!!!!!!!!!!!!!!!!!
//我在那磨了樣例半天想來想去都覺得1是2的1 / 2倍不太妥啊!!!!!!!!!
//突然覺得一個數,如果他們mod m同餘,那麼他們的作用就是一樣的.....那麼總共就只有m種數唄,就是他們不能放在一起...似乎是這樣!
// 6 % 4 = 2 10 % 4 = 2 (如果m是4)
//6 和 10放一起,做差就是±4,肯定是m的倍數
//如果把所有的狀態dp了肯定會炸 -> m維陣列擱這擱這呢?
//狀壓...用hash狀壓hhhhhhh
//c,爆搜解決一切!!!!!!!!!!!!!!!!
LL ans = 1;
LL fac[maxn];
LL n, m;
LL wmx[maxn];
LL cnt;
LL vis[maxn];
LL cont[maxn];//總共有i個人的類有幾個
LL lev[maxn];//剩下i個人的類有幾個
map <LL, LL> Map[35];
LL r = - 1;
LL base = 233;//得,開始雜湊了真的,這玩意真真就是hash的天堂啊,這不就是對映嘛!!!!!!!!
inline void init(LL &ans) {
    fac[0] = 1;
    for(Re i = 1; i <= n; i++) {
        fac[i] = i * fac[i - 1] % Mod;
    }
    for(Re i = 0; i <= cnt; i ++) {//當前人數為零的組也算(按上了)
        ans = ans * fac[cont[i]] % Mod;
    }
}

inline LL dfs(LL dpt, LL fa) {
    
    if(dpt > n)return 1;
    memset(lev, 0, 40 * sizeof(int));
    for(Re i = 0; i <= cnt; i++) {
        if(i != fa)lev[cont[i]]++;
    }
    LL l = cont[0];
    for(Re i = 1; i <= r; i ++) {l = l * base + lev[i];}
    if(fa)l = l * base + cont[fa];
    else l = l * base;
    if(Map[dpt].count(l))return Map[dpt][l];
    LL res = 0;
    //記得回溯啊!!!!!!!!
    if(cont[0] > 0) {
        cont[0] --;
        res = (res + dfs(dpt + 1, 0)) % Mod;
        cont[0] ++;
    }
    for(Re i = 1; i <= cnt; i ++) {
        if(i != fa && cont[i] > 0) {
            cont[i] --;
            res = (res + dfs(dpt + 1, i)) % Mod;
            cont[i] ++;
        }
    }
    Map[dpt][l] = res;
    return res;
}

signed main () { 
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    freopen("ssy.in", "r", stdin);
    freopen("ssy.out", "w", stdout);
    in(n);
    for(Re i = 1; i <= n; i ++)in(wmx[i]);
    in(m);
    for(Re i = 1; i <= n; i ++) {
        //while(wmx[i] < 0)wmx[i] += m;
        wmx[i] %= m;//分類
        if(wmx[i] < 0)wmx[i] += m;
    }
    for(Re i = 1; i <= n; i ++) {
        if(vis[i])continue;
        vis[i] = 1;
        LL tot = 1;
        for(Re j = i + 1; j <= n; j ++) {
            if(wmx[i] == wmx[j]){vis[j] = 1; tot++;}
        }
        if(tot == 1)cont[0]++;
        else cont[++cnt] = tot;
        r = max(r, tot);
    }
    init(ans);
    //cout << ans;
    cout << ans * dfs(1, 0) % Mod;
}

T4

雖然感覺我的dp寫的很假,但就是A了

點選檢視程式碼
#include <bits/stdc++.h>
#define Re register int
#define LL long long
#define ki cout << endl; 
#define WMX aiaiaiai~~

using namespace std;
namespace kiritokazuto{
	template <typename T> inline void in(T &x) {
		int f = 0; x = 0; char c = getchar();
		while(c < '0' || c > '9')f |= c == '-', c = getchar();
		while(c >= '0' && c <= '9')x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
		x = f ? -x : x;
	}
	template <typename T> inline void ot(T x) {
		if(x < 0)putchar('-'), x = -x;
		if(x > 9)ot(x / 10);putchar(x % 10 | '0');
	}
}
using namespace kiritokazuto;

const int maxn = 1e6 + 10;
const LL Mod = 1234567891, Inf = 2147483647;
struct Cow {
    LL t1, t2;
    LL wr;//WR亂入 -> worth
}wmx[maxn];
//沃ri,這玩意好像,竟然,似乎,是個DP
LL n, m, e;
LL dp[maxn];
bool cmp(Cow A, Cow B) {return A.t2 < B.t2;}
inline LL minn(LL x, LL y) {return (x > y) ? y : x;}
signed main () {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    freopen("clean.in", "r", stdin);
    freopen("clean.out", "w", stdout);
    in(n);
    in(m);
    in(e);
    for(Re i = 1; i <= n; i ++) {
        in(wmx[i].t1);
        in(wmx[i].t2);
        in(wmx[i].wr);
    }
    memset(dp, 0x7f, sizeof(dp));
    // LL ans = Inf;
    LL ans = dp[0];
    
    sort(wmx + 1, wmx + n + 1, cmp);
    for(Re i = 1; i <= n; i ++) {
        if(wmx[i].t1 == m) {dp[i] = wmx[i].wr;continue;}
        for(Re j = i - 1; wmx[j].t2 >= wmx[i].t1 - 1 && j >= 1; j --) {
            dp[i] = minn(dp[i], dp[j] + wmx[i].wr);//找它之前的牛中最便宜的(能續上弦的),再加上他自己工錢
        }
        if(wmx[i].t2 == e) {
            ans = minn(ans, dp[i]);
        }
    }
    if(ans == dp[0]) cout << "-1" << endl;
    else cout << ans;

}



# 賽後總結 :woshishabi