跳到正文

剪贴板

剪贴板 fuho8bap

原文
剪贴板信息

剪贴板信息

收录
更新

CSP2023 代码

CSP-J

T1:

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 1e5+5;
int n,tot,ans;
inline int rd()
{
    char c;int f = 1;
    while(!isdigit(c = getchar()))if(c=='-')f = -1;
    int x = c-'0';
    while(isdigit(c = getchar()))x = x*10+(c^48);
    return x*f;
}
int main()
{
    freopen("apple.in","r",stdin);
    freopen("apple.out","w",stdout);
    n = rd();
    while(n)
    {
        tot++;
        if(!ans&&n%3 == 1)ans = tot;
        n -= (n-1)/3+1;
    }
    cout << tot << ' ' << ans;
    return 0;
}

T2:

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 1e5+5;
int n;
ll sum[N],a[N],d,ans,res;
inline int rd()
{
    char c;int f = 1;
    while(!isdigit(c = getchar()))if(c=='-')f = -1;
    int x = c-'0';
    while(isdigit(c = getchar()))x = x*10+(c^48);
    return x*f;
}
int main()
{
    freopen("road.in","r",stdin);
    freopen("road.out","w",stdout);
    n = rd();d = rd();
    for(int i = 2;i <= n;i++)sum[i] = sum[i-1]+rd();
    for(int i = 1;i <= n;i++)a[i] = rd();
    int sh = 1;
    for(int i = 2;i <= n;i++)
        if(i == n||a[i] < a[sh])
        {
            ll dis = sum[i]-sum[sh];
            ll now = dis>res?(dis-res-1)/d+1:0;
            ans += now*a[sh];res += now*d-(dis);
            sh = i;
        }
    cout << ans;
    return 0;
}

T3:

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define ll long long
using namespace std;
const int N = 1e5+5;
int gcd(int x,int y){return y?gcd(y,x%y):x;}
struct node
{
    int p,q;
    void yue()
    {
        int x = gcd(p,q);p /= x;q /= x;
        if(q < 0)p = -p,q = -q;
    }
    void pri()
    {q == 1?printf("%d",p):printf("%d/%d",p,q);}
}f1,f2;
int get(int x)
{
    int y = sqrt(x)+0.5;
    for(int i = y;i;i--)
        if(x%(i*i)==0)return i;
    return 0;
}
inline int rd()
{
    char c;int f = 1;
    while(!isdigit(c = getchar()))if(c=='-')f = -1;
    int x = c-'0';
    while(isdigit(c = getchar()))x = x*10+(c^48);
    return x*f;
}
int main()
{
    freopen("uqe.in","r",stdin);
    freopen("uqe.out","w",stdout);
    int t = rd(),m = rd();
    for(int i = 1;i <= t;puts(""),i++)
    {
        int a = rd(),b = rd(),c = rd();
        if(a < 0)a = -a,b = -b,c = -c;
        int d = b*b-4*a*c;
        if(d < 0){printf("NO");continue;}
        int r = get(d);
        if(d == r*r){f1 = {-b+r,2*a};f1.yue();f1.pri();continue;}
        f1 = {-b,2*a};f1.yue();
        d /= r*r;f2 = {r,2*a};f2.yue();
        if(f1.p)f1.pri(),putchar('+');
        if(f2.p != 1)printf("%d*",f2.p);
        printf("sqrt(%d)",d);
        if(f2.q != 1)printf("/%d",f2.q);
    }
    return 0;
}

T4:

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <utility>
#define ll long long
using namespace std;
const int N = 1e4+5,K = 105,inf = 0x3f3f3f3f;
int hd[N],cnt,f[N][K],n,m,k;
bool vis[N][K];
struct node{int to,nex,w;}e[N << 1];
void add(int u,int v,int w)
{e[++cnt] = {v,hd[u],w};hd[u] = cnt;}
queue<pair<int,int> > q;
void spfa()
{
    memset(f,inf,sizeof f);f[1][0] = 0;
    vis[1][0] = 1;q.push(make_pair(1,0));
    while(!q.empty())
    {
        int u = q.front().first,x = q.front().second;
        q.pop();vis[u][x] = 0;
        for(int i = hd[u];i;i = e[i].nex)
        {
            int v = e[i].to,w = e[i].w,p = f[u][x],y = (x+1)%k;
            int c = p < w?p+((w-p-1)/k+1)*k:p;
            if(c+1 < f[v][y])
            {
                f[v][y] = c+1;
                if(!vis[v][y])vis[v][y] = 1,q.push(make_pair(v,y));
            }
        }
    }
}
inline int rd()
{
    char c;int f = 1;
    while(!isdigit(c = getchar()))if(c=='-')f = -1;
    int x = c-'0';
    while(isdigit(c = getchar()))x = x*10+(c^48);
    return x*f;
}
int main()
{
    freopen("bus.in","r",stdin);
    freopen("bus.out","w",stdout);
    n = rd();m = rd();k = rd();
    for(int i = 1;i <= m;i++)
    {int u = rd(),v = rd(),w = rd();add(u,v,w);}
    spfa();
    cout << (f[n][0]==inf?-1:f[n][0]);
    return 0;
}

CSP-S

T1:

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 1e5+5;
int f[10][10],a[10],n,ans;
bool check(int p)
{
    int x = 0,y = 0;
    for(int i = 1;i <= 5;i++)
        if(f[p][i] != a[i])
            if(!x)x = i;
            else if(!y)y = i;
            else return 0;
    if(!x||y&&y != x+1)return 0;
    return !y||(f[p][x]-a[x]+10)%10==(f[p][y]-a[y]+10)%10;
}
void dfs(int x)
{
    if(x == 6)
    {
        bool flag = 1;
        for(int i = 1;i <= n;i++)flag &= check(i);
        ans += flag;return ;
    }
    for(int i = 0;i <= 9;i++)a[x] = i,dfs(x+1);
}
inline int rd()
{
    char c;int f = 1;
    while(!isdigit(c = getchar()))if(c=='-')f = -1;
    int x = c-'0';
    while(isdigit(c = getchar()))x = x*10+(c^48);
    return x*f;
}
int main()
{
    freopen("lock.in","r",stdin);
    freopen("lock.out","w",stdout);
    n = rd();
    for(int i = 1;i <= n;i++)for(int j = 1;j <= 5;j++)
        f[i][j] = rd();
    dfs(1);cout << ans;
    return 0;
}

T2:

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <random>
#include <unordered_map>
#define ll long long
using namespace std;
const int N = 2e6+5,mod = 1e9+7;
int n;ll ans;
char s[N];
ll qp(ll x,int y)
{
    ll ans = 1;
    for(;y;y >>= 1,x = x*x%mod)
        if(y&1)(ans *= x) %= mod;
    return ans;
}
struct node
{
    int a[2][2];
    friend bool operator<(node x,node y)
    {
    	for(int i = 0;i < 2;i++)for(int j = 0;j < 2;j++)
    		if(x.a[i][j] != y.a[i][j])return x.a[i][j] < y.a[i][j];
    	return 0;
	}
    friend node operator*(node x,node y)
    {
        node z;
        for(int i = 0;i < 2;i++)for(int j = 0;j < 2;j++)
            z.a[i][j] = (1ll*x.a[i][0]*y.a[0][j]+1ll*x.a[i][1]*y.a[1][j])%mod;
        return z;
    }
    void pri()
    {printf("%d %d\n%d %d\n",a[0][0],a[0][1],a[1][0],a[1][1]);}
}f[26],sum;
unordered_map<ll,int> m;
ll zhuan(node x)
{return x.a[0][0]*343434343ll+x.a[0][1]*1145141918ll+x.a[1][0]*1234567891ll+x.a[1][1]*2198765436ll;}
mt19937 rnd(time(0));
inline int rd()
{
    char c;int f = 1;
    while(!isdigit(c = getchar()))if(c=='-')f = -1;
    int x = c-'0';
    while(isdigit(c = getchar()))x = x*10+(c^48);
    return x*f;
}
int main()
{
    freopen("game.in","r",stdin);
    freopen("game.out","w",stdout);
    for(int i = 0;i < 26;i++)
    {
        int x = rnd()%(mod-1)+1,y = rnd()%(mod-1)+1;
        f[i] = {x,y,(1-1ll*x*x%mod+mod)*qp(y,mod-2)%mod,mod-x};
    }
    n = rd();scanf("%s",s+1);
    m[zhuan(sum = {1,0,0,1})] = 1;
    for(int i = 1;i <= n;i++)
        sum = sum*f[s[i]-'a'],ans += m[zhuan(sum)]++;
    cout << ans;
    return 0;
}

T3:

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
const int N = 1e5+5;
struct node
{
    ll siz;int lim;
    int n;ll l[105],r[105];
    string tp[105],nam[105];
};
map<string,node> m;
int cnt;
ll l[105],r[105];
string Tp[105],Nam[105];
string find(node x,ll add)
{
    if(!x.n)return "";
    for(int i = 1;i <= x.n;i++)
        if(x.l[i] <= add&&add <= x.r[i])
            return '.'+x.nam[i]+find(m[x.tp[i]],add-x.l[i]);
    return "~";
}
inline ll rd()
{
    char c;int f = 1;
    while(!isdigit(c = getchar()))if(c=='-')f = -1;
    ll x = c-'0';
    while(isdigit(c = getchar()))x = x*10+(c^48);
    return x*f;
}
bool check(char c){return c=='.'||'a' <= c&&c <= 'z';}
inline string rdstr()
{
    char c;while(!check(c = getchar()));
    string s;s += c;
    while(check(c = getchar()))s += c;
    return s;
}
int main()
{
    freopen("struct.in","r",stdin);
    freopen("struct.out","w",stdout);
    m["byte"] = {1,1,0};m["short"] = {2,2,0};
    m["int"] = {4,4,0};m["long"] = {8,8,0};
    int t = rd();
    while(t--)
    {
        int op = rd();
        if(op == 1)
        {
            string s = rdstr();int n;
            node p = {0,0,n = rd()};
            for(int i = 1;i <= n;i++)
            {
                string tp;
                p.tp[i] = tp = rdstr();p.nam[i] = rdstr();
                int nlim = m[tp].lim;ll nsiz = m[tp].siz;
                p.lim = max(p.lim,nlim);
                p.l[i] = i==1?0:(p.r[i-1]/nlim+1)*nlim;
                p.r[i] = p.l[i]+nsiz-1;
            }
            p.siz = (p.r[n]/p.lim+1)*p.lim;
            m[s] = p;printf("%lld %d\n",p.siz,p.lim);
        }
        else if(op == 2)
        {
            string tp,nam;
            Tp[++cnt] = tp = rdstr();Nam[cnt] = nam = rdstr();
            int nlim = m[tp].lim;ll nsiz = m[tp].siz;
            l[cnt] = cnt==1?0:(r[cnt-1]/nlim+1)*nlim;r[cnt] = l[cnt]+nsiz-1;
            printf("%lld\n",l[cnt]);
        }
        else if(op == 3)
        {
            string s = rdstr(),now;ll addr = 0;
            node sh;bool flag = 1;
            for(int i = 0;i < s.size();i++)
            {
                if(s[i] != '.')now += s[i];
                if(s[i] == '.'||i == s.size()-1)
                    if(flag)
                    {
                        for(int j = 1;j <= cnt;j++)
                            if(Nam[j] == now)
                            {addr += l[j];sh = m[Tp[j]];break;}
                        flag = 0;now = "";continue;
                    }
                    else
                    {
                        for(int j = 1;j <= sh.n;j++)
                            if(sh.nam[j] == now)
                            {addr += sh.l[j];sh = m[sh.tp[j]];break;}
                        now = "";
                    }
            }
            printf("%lld\n",addr);
        }
        else
        {
            ll addr = rd();
            string ans = "~";
            for(int i = 1;i <= cnt;i++)
                if(l[i] <= addr&&addr <= r[i])
                {ans = Nam[i]+find(m[Tp[i]],addr-l[i]);break;}
            if(ans[ans.size()-1] == '~')puts("ERR");
            else cout << ans << endl;
        }
    }
    return 0;
}

T4:T4就写了个随机数,这里就不放辣。

NOIP2023 代码

T1:

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 3005;
int a[N][30],mi = 1,sum,n,m,cnt;
char s[N],t[N];
void update(int i)
{
    for(int j = 25;~j;j--)
        if(a[i][j] < a[mi][j]){mi = i;sum = 1;return ;}
        else if(a[i][j] > a[mi][j])return ;
    sum++;
}
bool cmp()
{
    for(int i = 1;i <= m;i++)
        if(s[i] != t[i])return s[i] < t[i];
    return sum == 1;
}
inline int rd()
{
    char c;int f = 1;
    while(!isdigit(c = getchar()))if(c=='-')f = -1;
    int x = c-'0';
    while(isdigit(c = getchar()))x = x*10+(c^48);
    return x*f;
}
int main()
{
    freopen("dict.in","r",stdin);
    freopen("dict.out","w",stdout);
    n = rd();m = rd();
    for(int i = 1;i <= n;i++)
    {
        scanf("%s",s+1);
        for(int j = 1;j <= m;j++)a[i][s[j]-'a']++;
        update(i);
    }
    for(int j = 25;~j;j--)
        for(int k = 1;k <= a[mi][j];k++)
            t[++cnt] = 'a'+j;
    for(int i = 1;i <= n;i++)
    {
        cnt = 0;
        for(int j = 0;j < 26;j++)
            for(int k = 1;k <= a[i][j];k++)
                s[++cnt] = 'a'+j;
        printf("%d",cmp());
    }
    return 0;
}

T2:

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 1e5+5;
int f[N],siz[N],g[N],n,m,ans;
bool flag[N];
struct node{int tp,x;}a[N];
node ff(node x)
{return x.tp==2?(node){2,~x.x?!x.x:-1}:(node){!x.tp,x.x};}
int fd(int x){return x==f[x]?x:f[x] = fd(f[x]);}
void hb(int x,int y)
{
    x = fd(x);y = fd(y);
    if(x&&y&&x!=y)f[y] = x,siz[x] += siz[y];
}
inline int rd()
{
    char c;int f = 1;
    while(!isdigit(c = getchar()))if(c=='-')f = -1;
    int x = c-'0';
    while(isdigit(c = getchar()))x = x*10+(c^48);
    return x*f;
}
inline char rdc()
{
    char c;
    while((c = getchar()) != '-'&&c != '+'&&(c < 'A'||c > 'Z'));
    return c;
}
int main()
{
    freopen("tribool.in","r",stdin);
    freopen("tribool.out","w",stdout);
    int c = rd(),t = rd();
    while(t--)
    {
        n = rd();m = rd();ans = 0;
        for(int i = 1;i <= n;i++)
            a[i] = {1,i},f[i] = i,siz[i] = 1,flag[i] = g[i] = 0;
        while(m--)
        {
            char op = rdc();int i = rd();
            if('A' <= op&&op <= 'Z')
                a[i] = {2,op=='U'?-1:op=='T'?1:0};
            else if(op == '+')a[i] = a[rd()];
            else a[i] = ff(a[rd()]);
        }
        for(int i = 1;i <= n;i++)
            if(a[i].tp == 1)
            {
                int x = fd(i),y = fd(a[i].x),fx = fd(g[x]),fy = fd(g[y]);
                hb(x,y);hb(fx,fy);int z = fx?fx:fy;
                if(z)g[x] = z;g[z] = x;
            }
            else if(!a[i].tp)
            {
                int x = fd(i),y = fd(a[i].x),fx = fd(g[x]),fy = fd(g[y]);
                hb(x,fy);hb(y,fx);g[x] = y;g[y] = x;
            }
        for(int i = 1;i <= n;i++)
        {
            int x = fd(i),fx = fd(g[x]);
            if(x == fx)flag[x] = 1;
            if(a[i].tp==2&&a[i].x==-1)
                flag[x] = flag[fx] = 1;
        }
        for(int i = 1;i <= n;i++)
            if(i == f[i]&&flag[i])ans += siz[i];
        printf("%d\n",ans);
    }
    return 0;
}

T3:

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 2005;
int a[N],b[N],aa[N],bb[N],n,m,q;
bool f[N][N];
bool solve()
{
    f[1][0] = 1;
    for(int i = 1;i <= n;i++)for(int j = 1;j <= m;j++)
        f[i][j] = a[i] != b[j]&&(a[i] > b[j])^(a[1] < b[1]);
    for(int i = 1;i <= n;i++)for(int j = 1;j <= m;j++)
        f[i][j] &= f[i-1][j]|f[i][j-1];
    return f[n][m];
}
inline int rd()
{
    char c;int f = 1;
    while(!isdigit(c = getchar()))if(c=='-')f = -1;
    int x = c-'0';
    while(isdigit(c = getchar()))x = x*10+(c^48);
    return x*f;
}
int main()
{
    freopen("expand.in","r",stdin);
    freopen("expand.out","w",stdout);
    int c = rd();n = rd();m = rd();q = rd();
    if(c > 7)
    {
        for(int i = 1;i <= q+1;i++)putchar('1');
        return 0;
    }
    for(int i = 1;i <= n;i++)a[i] = rd();
    for(int i = 1;i <= m;i++)b[i] = rd();
    printf("%d",solve());
    memcpy(aa,a,sizeof a);memcpy(bb,b,sizeof b);
    while(q--)
    {
        memcpy(a,aa,sizeof aa);memcpy(b,bb,sizeof bb);
        int nn = rd(),mm = rd();
        while(nn--){int id = rd();a[id] = rd();}
        while(mm--){int id = rd();b[id] = rd();}
        printf("%d",solve());
    }
    return 0;
}

T4:

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#define ll long long
using namespace std;
const int N = 1005,M = 1e5+5;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int n,m,k,d;
ll f[N][N],f2[M][2],ans;
vector<pair<int,int> > v[N];
inline int rd()
{
    char c;int f = 1;
    while(!isdigit(c = getchar()))if(c=='-')f = -1;
    int x = c-'0';
    while(isdigit(c = getchar()))x = x*10+(c^48);
    return x*f;
}
int main()
{
    freopen("run.in","r",stdin);
    freopen("run.out","w",stdout);
    int c = rd(),q = rd();
    while(q--)
    {
        n = rd();m = rd();k = rd();d = rd();
        if(c > 9)
        {
            ans = 0;
            for(int i = 1;i <= m;i++)
            {
                ll x = rd(),y = rd(),val = rd();
                ans += max(0ll,val-y*d);
            }
            printf("%lld\n",ans);continue;
        }
        for(int i = 1;i <= n;i++)v[i].clear();
        while(m--)
        {
            int x = rd(),y = rd(),val = rd();
            v[x].push_back(make_pair(y,val));
        }
        ans = -inf;
        for(int i = 1;i <= n;i++)
        {
            int up = min(i,k);
            for(int j = 0;j <= up;j++)f[i][j] = 0;
            for(pair<int,int> x:v[i])f[i][x.first] += x.second;
            for(int j = 1;j <= up;j++)f[i][j] += f[i][j-1];

            for(int j = 1;j <= up;j++)f[i][j] += f[i-1][j-1]-d;
            for(int j = 0;j <= min(i-1,k);j++)
                f[i][0] = max(f[i][0],f[i-1][j]);
            if(i == n)for(int j = 0;j <= up;j++)
                ans = max(ans,f[n][j]);
        }
        printf("%lld\n",ans);
    }
    return 0;
}