CSP-S开小灶1 && CSP-S加赛1

居然敢嫌弃Cat的文化课不让Cat来参加半集训!?哼这是不可能的Cat哭给你看!……

A. ZZH的游戏

WA 15:emmm想到了二分,然后判断的时候我没有想到让它贪心地走到尽量小的节点,而是让它尽量跳father,我怕它尽量往小跳反而偏离了终点结束不了游戏,跳不了了就让另一棵树后退到最小的儿子(这或许是WA的原因,因为最小儿子的最小儿子不一定比次小儿子的最小儿子小,但总之还是骗到了15分),以1为根建树当然跳到根游戏就结束了。

是的是的是的我又鹤了%%%Chen_jr的题解

一开始看到正解上的从s+t开始不行了+1就好迷迷糊糊,这怎么可能比二分快,鹤完代码才知道这个+1指的是在过程中加,意思就是每组数据大模拟只有一次。

保存每个点能到达的最小位置,用它直接作为另一棵树拓展的条件,可以避免会T的后退操作,所以可以要求每个点最多经过一次。

#include <bits/stdc++.h>
  
using namespace std;
  
typedef long long ll;
const int maxn = 1e6 + 2;
const int N = 60;
const ll mod = 1e9 + 7;

int T, s, t, n;

inline int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')
        {
            f = -1;
        }
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        x = (x << 1) + (x << 3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}

struct tree 
{
    struct edge 
    {
        int next, to;
    }a[maxn<<1];
    int head[maxn], len;
    void add(int x, int y)
    {
        a[++len].to = y; a[len].next = head[x]; head[x] = len;
    }
    bool vis[maxn];
    int mi = maxn + maxn + maxn;
    priority_queue<int, vector<int>, greater<int> > q;
    void clear()
    {
        for(int i=1; i<=n; i++) head[i] = 0;
        for(int i=1; i<=n; i++) vis[i] = 0;
        len = 0; mi = maxn + maxn + maxn;
        while(!q.empty()) q.pop();
    }
    void build()
    {
        for(int i=1; i<n; i++)
        {
            int x = read(), y = read();
            add(x, y); add(y, x);
        }
    }
    bool expand(int mx)
    {
        bool flag = 0;
        while(!q.empty())
        {
            int x = q.top(); 
            if(x > mx) break;
            q.pop(); vis[x] = 1; flag = 1; mi = min(mi, x);
            for(int i=head[x]; i; i=a[i].next)
            {
                int v = a[i].to; 
                if(vis[v]) continue;
                q.push(v);
            }
        }
        return flag;
    }
    void set(int x) {mi = x; q.push(x);}
}t1, t2;

int solve()
{
    n = read(); t1.build(); t2.build();
    s = read(); t = read();
    int ans = s + t;
    t1.set(s); t2.set(t);
    t1.expand(s); t2.expand(t);
    while(1)
    {
        bool f1 = 1, f2 = 1;
        while(f1 || f2)
        {
            if(t1.mi == 1 && t2.mi == 1) return ans;
            f1 = t1.expand(ans-t2.mi);
            f2 = t2.expand(ans-t1.mi);
        }
        ans++;
    }
}

int main()
{
    T = read();
    while(T--)
    {
        printf("%d\n", solve());
        t1.clear(); t2.clear();
    }
  
    return 0;
}
View Code

 

B. ZZH与背包

枚举一下每个物品选或不选TLE11***鹤个题解去……

盲猜数据被改了,要不然为什么变成了96***过了的都是20个点,现在变成了27个,好坑哦……

不过即使被卡了还是要%%%,我才知道还可以预处理log这样就可以减少一些循环,还有数据范围比可以状压的范围大的时候可以把它拆了状压两次!Miracle!

一看到双指针就莫名联想到cdq分治,那个什么电话基站的题好像因为中间的部分没法确定就把左右边界全都改成了右边界。emmm不过还是关系不大,用双指针的多了不是cdq的专属……

#include <bits/stdc++.h>
  
using namespace std;
  
typedef long long ll;
const int maxn = 50;
const int N = 60;
const ll mod = 1e9 + 7;

int n, q, v[maxn], m1, m2, n1, n2, lg[(1<<20)+5];
ll l, r, a[(1<<20)+5], b[(1<<20)+5];

inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')
        {
            f = -1;
        }
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        x = (x << 1) + (x << 3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}

ll solve(ll mv)
{
    ll ans = 0; int r = 0;
    for(int l=m1-1; l>=0; l--)
    {
        while(r < m2 && a[l]+b[r]<=mv) r++;
        ans += r;
    }
    return ans;
}

int main()
{
    n = read(); q = read();
    n1 = n / 2, n2 = n - n1;
    for(int i=1; i<=n1; i++) v[i] = read();
    m1 = 1 << n1;
    lg[1] = 0;
    for(int i=1; i<=n2; i++) lg[1<<i] = i;
    for(int i=0; i<m1; i++)
    {
        int now = i;
        for(int j=now; j; j-=(j&-j)) a[i] += v[lg[j&-j]+1];
    }
    sort(a+1, a+m1);
    for(int i=1; i<=n2; i++) v[i] = read();
    m2 = 1 << n2;
    for(int i=0; i<m2; i++)
    {
        int now = i;
        for(int j=now; j; j-=(j&-j)) b[i] += v[lg[j&-j]+1];
    }
    sort(b+1, b+m2);
    while(q--)
    {
        l = read(); r = read();
        printf("%lld\n", solve(r)-solve(l-1));
    }
  
    return 0;
}
TLE 95 继续鹤题解去

 优化好像就是把r和l一起求,因为l一定比r小嘛
还有就是预处理那个和:就和竞赛图一样

#include <bits/stdc++.h>
  
using namespace std;
  
typedef long long ll;
const int maxn = 50;
const int N = 60;
const ll mod = 1e9 + 7;

ll n, m, ans, v[maxn], n1, n2, mx1, mx2, f[(1<<20)+5], g[(1<<20)+5];

inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')
        {
            f = -1;
        }
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        x = (x << 1) + (x << 3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}

int main()
{
    n = read(); m = read();
    for(int i=1; i<=n; i++) v[i] = read();
    n1 = (n>>1), mx1 = (1<<n1);
    for(int i=0; i<mx1; i++)
    {
        for(int j=1; j<=n1; j++)
        {
            int k = (1<<(j-1));
            if(i & k) continue;
            f[i|k] = f[i] + v[j];
        }
    }
    n2 = n - n1, mx2 = (1<<n2);
    for(int i=0; i<mx2; i++)
    {
        for(int j=n1+1; j<=n; j++)
        {
            int k = 1<<(j-n1-1);
            if(i & k) continue;
            g[i|k] = g[i] + v[j];
        }
    }
    sort(f, f+mx1); sort(g, g+mx2);
    while(m--)
    {
        ll l = read(), r = read(), lq = mx2-1, rq = mx2-1;
        ans = 0;
        for(int i=0; i<mx1; i++)
        {
            while(rq>=0 && g[rq]+f[i]>r) rq--;
            while(lq>=0 && g[lq]+f[i]>=l) lq--;
            ans += rq - lq;
        }
        printf("%lld\n", ans);
    }
   
    return 0;
}
View Code

 

A. antipalindrome

我本来在想:不回文的话把回文的减掉就好了,每个长度的回文串的种类就是它的长度/2向上取整再m次方,好像还需要容斥原理什么的……搞不对……翻开题解就看见了“签到题”几个大字*****Cat怎么这么菜啊

不回文的方案可以直接考虑,每个回文串必定存在相邻两个字母相同或隔一个字母,从左到右把每个数填进去要考虑不和左边第一个相同也不和左边第二个相同,答案就是m*(m-1)*qpow(m-2, n-2)。

得到大佬的提示把m取模了,但是没有加特判比如n=1,n=2,m=1之类的TLE 0,超时是因为指数变成了负数,采用右移操作的话,向下取整-1还是-1就死循环了……

#include <bits/stdc++.h>
  
using namespace std;
  
typedef long long ll;
const int maxn = 1e6 + 2;
const int N = 60;
const ll mod = 1e9 + 7;

ll T, n, m, ans;

inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')
        {
            f = -1;
        }
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        x = (x << 1) + (x << 3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}

ll qpow(ll a, ll b)
{
    ll ans = 1;
    while(b)
    {
        if(b & 1) ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}

int main()
{
    T = read();
    while(T--)
    {
        n = read(); m = read() % mod;
        if(n == 1) ans = m;
        else if(n == 2) ans = m * (m-1) % mod;
        else if(m == 1) ans = 0;
        else ans = m * (m-1) % mod * qpow(m-2, n-2) % mod;
        printf("%lld\n", ans);
    }
  
    return 0;
}
View Code

 

B. ZZH与计数

对于这么一个阴间题,改个暴力得了……

注意转移的时候,不需要继承,完全由上一轮的值乘概率算出来,搞什么减一之类的怎么都减不对,可能本来就不能那么算。所以为了既保留上一轮的值,又使当前轮初值为0,还要保证空间,就要用到滚动数组。m&1 == ! ((m-1)&1)

然后就是,积累一个函数:__builtin_popcount(i) 可以计算二进制下i里的1的个数。

#include <bits/stdc++.h>
  
using namespace std;
  
typedef long long ll;
const int maxn = 1e6 + 2;
const int N = 60;
const ll mod = 998244353;

int ans[(1<<17)+5], ipo[(1<<17)+5], ct[(1<<17)+5], n, m, a, b;
int f[2][(1<<17)+5], mx, p, ip, v[(1<<17)+5];

inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')
        {
            f = -1;
        }
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        x = (x << 1) + (x << 3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}

ll qpow(ll a, ll b)
{
    ll ans = 1;
    while(b)
    {
        if(b & 1) ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}

void init()
{
    mx = (1<<n);
    ipo[0] = 1; for(int i=1; i<=n; i++) ipo[i] = qpow(1<<i, mod-2);
    for(int i=1; i<mx; i++) ct[i] = __builtin_popcount(i);
    p = 1ll*a*qpow(b, mod-2)%mod;
    ip = 1ll*(b-a)*qpow(b, mod-2)%mod;
}

void work()
{
    if(m == 0) 
    {
        for(int i=0; i<mx; i++) ans[i] = v[i];
        return;
    }
    for(int i=0; i<mx; i++) f[0][i] = v[i];
    for(int lp=0; lp<m; lp++)
    {
        int d = lp & 1;
        for(int i=0; i<mx; i++)
        {
            if(f[d][i])
            {
                int np = 1ll*ipo[ct[i]]*p%mod*f[d][i]%mod;
                for(int j=i; j; j=i&(j-1)) f[!d][j] = (f[!d][j]+np)%mod;
                f[!d][0] = (f[!d][0]+np)%mod;
                np = 1ll*ipo[n-ct[i]]*ip%mod*f[d][i]%mod;
                int nt = (mx-1)^i;
                for(int j=nt; j; j=nt&(j-1)) f[!d][j|i] = (f[!d][j|i]+np)%mod;
                f[!d][i] = (f[!d][i]+np)%mod;
            }
        }
        memset(f[d], 0, sizeof(f[d]));
    }
    for(int i=0; i<mx; i++) ans[i] = f[m&1][i];
}

int main()
{
    n = read(); m = read(); a = read(); b = read();
    init();
    for(int i=0; i<mx; i++)
    {
        v[i] = read();
    }
    work();
    for(int i=0; i<mx; i++)
    {
        printf("%d ", ans[i]);
    }
  
    return 0;
}
TLE 15

 

posted @ 2022-09-05 20:14  Catherine_leah  阅读(51)  评论(0编辑  收藏  举报
/* */