QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#318846#7607. The Doubling Game 2arnold518WA 12ms61472kbC++173.4kb2024-02-01 00:26:212024-02-01 00:26:23

Judging History

你现在查看的是最新测评结果

  • [2024-02-01 00:26:23]
  • 评测
  • 测评结果:WA
  • 用时:12ms
  • 内存:61472kb
  • [2024-02-01 00:26:21]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 3e5;

const int MOD = 1e9+7;

struct mint
{
    int x;
    mint() : x(0) {}
    mint(int x) : x(x) {}
    mint operator + (int ot) const { return x+ot>=MOD ? x+ot-MOD : x+ot; }
    mint operator - (int ot) const { return x<ot ? x+MOD-ot : x-ot; }
    mint operator - () const { return x ? MOD-x : 0; }
    mint operator * (int ot) const { return 1ll*x*ot%MOD; }
    mint operator += (int ot) { return *this = *this + ot; }
    mint operator -= (int ot) { return *this = *this - ot; }
    mint operator *= (int ot) { return *this = *this * ot; }
    operator int() const { return x; }
};

int N;
vector<int> adj[MAXN+10];
int sz[MAXN+10];

mint dp1[MAXN+10];
mint dp2[MAXN+10][21];
mint dp3[MAXN+10][21];

int clz(int x)
{
    if(!x) return 32;
    return __builtin_clz(x);
}

void dfs(int now, int bef)
{
    sz[now]=1;
    vector<int> chd;
    for(int nxt : adj[now])
    {
        if(nxt==bef) continue;
        dfs(nxt, now);
        sz[now]+=sz[nxt];
        chd.push_back(nxt);
    }

    sort(chd.begin(), chd.end(), [&](const int &p, const int &q) { return sz[p]<sz[q]; });

    vector<mint> V[2];
    V[0].push_back(1);
    V[1].push_back(0);
    for(int i=0; i<chd.size(); i++)
    {
        int nxt=chd[i];
        int k=__lg(sz[nxt])+1;
        if(i+1==chd.size()) k=min(k, __lg((int)V[0].size())+2);
        k=min(k, 20);
        vector<mint> V2[2];
        V2[0]=V2[1]=vector<mint>(1<<k);
        for(int j=0; j<(1<<k); j++)
        {
            if(j<V[0].size()) V2[0][j]+=V[0][j]*dp1[nxt];
            for(int p=0; p<k; p++) if((j&(1<<p)) && (j^(1<<p))<V[0].size()) V2[0][j]+=V[0][j^(1<<p)]*dp2[nxt][p];
        }
        for(int j=0; j<(1<<k); j++)
        {
            if(j<V[0].size()) V2[1][j]+=V[1][j]*dp1[nxt];
            for(int p=0; p<k; p++) if((j&(1<<p)) && (j^(1<<p))<V[1].size() && clz(j)==clz(j^(1<<p))) V2[1][j]+=V[1][j^(1<<p)]*dp2[nxt][p];
            for(int p=0; p<k; p++)
            {
                if(j&(1<<p) && (j^(1<<p))<V[0].size() && clz(j)!=clz(j^(1<<p))) V2[1][j]+=V[0][j^(1<<p)]*dp3[nxt][p];
            }
        }

        V[0]=V2[0];
        V[1]=V2[1];
    }

    int k=min(__lg((int)sz[now])+1, 20);
    for(int p=0; p<=k && (1<<p)-1<V[0].size(); p++)
    {
        dp1[now]+=V[0][(1<<p)-1];
        dp1[now]+=V[1][(1<<p)-1];
        
        dp2[now][p]+=V[0][(1<<p)-1];
    }
    for(int p=0; p<=k; p++)
    {
        if((1<<p)-1<V[0].size()) dp3[now][p]+=V[0][(1<<p)-1];
        for(int q=p+2; q<=k+1; q++) if((((1<<q)-1)^(1<<p))<V[1].size()) dp3[now][p]+=V[1][((1<<q)-1)^(1<<p)];
    }

    // printf("NOW %d\n", now);
    // for(int i=0; i<V[0].size(); i++) printf("%d ", (int)V[0][i]);
    // printf(" : V0\n");
    // for(int i=0; i<V[1].size(); i++) printf("%d ", (int)V[1][i]);
    // printf(" : V1\n");
    // printf("%d : DP1\n", (int)dp1[now]);
    // for(int i=0; i<=k; i++) printf("%d ", (int)dp2[now][i]);
    // printf(" : DP2\n");
    // for(int i=0; i<=k; i++) printf("%d ", (int)dp3[now][i]);
    // printf(" : DP3\n\n");
}

int main()
{
    scanf("%d", &N);
    for(int i=1; i<N; i++)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1, 1);
    printf("%d\n", (int)dp1[1]);
}

詳細信息

Test #1:

score: 100
Accepted
time: 12ms
memory: 61472kb

input:

5
1 2
1 3
1 4
4 5

output:

21

result:

ok single line: '21'

Test #2:

score: 0
Accepted
time: 0ms
memory: 61268kb

input:

1

output:

1

result:

ok single line: '1'

Test #3:

score: -100
Wrong Answer
time: 4ms
memory: 61224kb

input:

128
11 32
116 81
65 4
117 47
5 81
104 30
61 8
82 59
95 20
92 29
29 127
97 39
123 33
59 128
115 33
83 67
74 16
77 33
64 73
124 123
8 127
61 51
101 122
35 90
119 116
112 27
81 93
109 123
54 1
119 100
116 16
65 47
67 27
22 105
76 87
36 39
27 96
72 31
91 123
21 105
118 12
110 48
121 72
14 115
24 16
106 ...

output:

283150188

result:

wrong answer 1st lines differ - expected: '508800953', found: '283150188'