QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#504127#2918. Tree Number GeneratorZhouShangWA 5ms16192kbC++202.1kb2024-08-04 08:11:472024-08-04 08:11:51

Judging History

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

  • [2024-08-04 08:11:51]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:16192kb
  • [2024-08-04 08:11:47]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define rep(i,a,b) for (int i = a; i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define PB push_back
#define FS first
#define SD second
#define ary(k) array<int, k>
template<class A, class B> void cmx(A &x, B y) { x = max<A>(x, y);}
template<class A, class B> void cmn(A &x, B y) { x = min<A>(x, y);}
typedef pair<int, int> pii;
typedef vector<int> vi;
int n,m,t,a[1<<20],p[1<<20],q[1<<20][21],f[1<<20][21],d[1<<20];
vector<int>e[1<<20];
int P(int x,int y){
    int z=1;
    for(;y;y/=2,x=x*x%m)
        if(y&1)
            z=z*x%m;
    return z;
}
void D(int x){
    for(int y:e[x])
        if(y!=f[x][0])
            d[y]=d[x]+1,f[y][0]=x,p[y]=(p[x]*10+a[y])%m,q[y][0]=a[y],D(y);
}
int L(int x,int y){
    if(d[x]<d[y])
        swap(x,y);
    for(int i=20;~i;i--)
        if(d[f[x][i]]>=d[y])
            x=f[x][i];
    if(x==y)
        return x;
    for(int i=20;~i;i--)
        if(f[x][i]!=f[y][i])
            x=f[x][i],y=f[y][i];
    return f[x][0];
}
int W(int x,int y){
    return ((p[y]-p[f[x][0]]*P(10,d[y]-d[x]+1))%m+m)%m;
}
int U(int x,int y){
    int z=0;
    for(int i=20;~i;i--)
        if(d[f[x][i]]>=d[y])
            (z+=q[x][i]*P(10,d[f[x][i]]-d[y]+1)),x=f[x][i];
    return ((z+a[y])%m+m)%m;
}
signed main() {
    cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit);
    cin>>n>>m>>t;
    for(int i=1,x,y;i<n;i++)
        cin>>x>>y,e[x].push_back(y),e[y].push_back(x);
    for(int i=1;i<=n;i++)
        cin>>a[i],a[i]%=m;
    p[1]=q[1][0]=a[1],d[1]=1,D(1);
    for(int i=1;i<=20;i++)
        for(int j=1;j<=n;j++){
            f[j][i]=f[f[j][i-1]][i-1];
            q[j][i]=q[j][i-1]*P(10,(1<<(i-1)))+q[f[j][i-1]][i-1];
        }
    for(int _=1,x,y;_<=t;_++){
        cin>>x>>y;
        int z=L(x,y);
        if(z==x)
            cout<<W(x,y)<<'\n';
        else if(z==y)
            cout<<U(x,y)<<'\n';
        else
            cout<<((U(x,z)*P(10,d[y]-d[z])+W(z,y)-a[z]*P(10,d[y]-d[z]))%m+m)%m<<'\n';
    }
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 11824kb

input:

2 1 4
1 2
1
3
1 2
2 1
1 1
2 2

output:

0
0
0
0

result:

ok 4 lines

Test #2:

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

input:

3 10 5
1 2
2 3
0
0
0
1 3
3 1
2 2
2 3
2 1

output:

0
0
0
0
0

result:

ok 5 lines

Test #3:

score: 0
Accepted
time: 5ms
memory: 12224kb

input:

2000 2 2000
937 1471
1672 937
356 1672
976 356
1257 356
1503 1257
783 1503
1783 937
1284 976
1955 1503
802 1257
583 1471
526 356
701 1783
393 1955
307 1955
386 1955
1070 937
1724 802
1397 1724
1140 937
422 526
1941 1955
1638 937
1469 526
1800 526
1035 1800
1009 1140
1195 1800
142 1471
1225 1469
1524...

output:

0
1
1
1
0
0
1
0
0
1
0
1
0
0
1
1
0
0
1
1
1
1
1
0
0
0
1
0
1
1
1
1
1
1
0
1
1
1
0
1
1
1
0
1
1
1
0
1
1
1
1
0
0
0
1
1
0
1
0
0
0
0
0
1
0
1
1
0
0
0
0
1
1
1
1
1
0
0
1
1
1
0
1
1
1
0
0
1
1
1
1
0
0
0
1
0
1
0
0
1
1
1
0
1
1
0
1
0
1
0
0
1
0
1
0
0
1
1
0
1
1
1
1
0
0
0
1
0
0
0
0
1
1
0
1
1
0
1
1
0
1
1
0
0
0
1
1
0
0
1
...

result:

ok 2000 lines

Test #4:

score: 0
Accepted
time: 3ms
memory: 16192kb

input:

2000 1000 2000
1664 1028
763 1664
1541 1028
1544 1664
69 1544
1473 1541
475 1541
1551 1541
579 1473
262 475
1777 475
1916 475
391 763
807 1028
1357 1028
1682 69
345 1544
234 1541
63 391
480 807
1762 1544
306 1916
1436 1777
891 391
1852 306
523 1852
264 475
313 306
1139 391
1792 69
1604 69
398 313
10...

output:

263
429
976
56
31
940
168
28
487
658
273
218
944
664
498
705
709
490
61
931
421
664
632
538
876
282
145
61
430
984
589
436
780
641
69
126
625
208
629
603
566
57
355
843
705
781
514
898
804
290
366
642
429
899
716
466
371
620
252
606
690
500
412
226
495
380
61
580
805
132
423
845
618
862
924
729
637
...

result:

ok 2000 lines

Test #5:

score: 0
Accepted
time: 2ms
memory: 12288kb

input:

2000 1000000 2000
676 154
686 154
357 154
187 676
299 686
1508 357
1515 1508
972 686
105 357
748 1515
1711 686
1692 154
1869 299
1017 187
829 1017
809 1515
1505 676
383 1515
1002 972
1448 829
1657 1515
1824 1508
1271 1711
545 1515
1099 748
1255 748
556 545
388 1017
1290 357
992 1824
66 1017
1812 972...

output:

911946
658749
941314
251735
719871
241911
734055
143108
569168
899637
173402
264918
271418
632775
991506
402910
517268
914587
379978
462220
622382
658742
329239
43729
56506
192359
410979
57536
866374
142798
124989
947854
413121
183893
602943
488815
292373
183960
602947
199025
301900
603305
260397
64...

result:

ok 2000 lines

Test #6:

score: -100
Wrong Answer
time: 5ms
memory: 12328kb

input:

2000 1000000000 2000
352 747
534 747
294 534
1548 534
1051 534
1850 747
574 1850
43 1548
1395 1850
1615 1051
1236 294
1869 43
1039 1051
288 294
299 747
217 1395
1220 1051
622 1850
673 574
1296 288
1331 1296
721 1039
1062 1615
518 721
1276 1296
1806 1276
1731 1869
477 574
461 1051
1703 622
358 1703
1...

output:

79101007
887079978
189124001
400049688
284
43253539
818072051
53354038
9042495
272009043
500270860
700462128
335009411
350090556
872000107
46209971
707883989
53354276
271083550
900288054
2682651
818832941
27070294
70720415
513017327
230170729
271033998
200904807
27104651
347046254
900272592
70720094...

result:

wrong answer 455th lines differ - expected: '90788648', found: '987556648'