QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#472932 | #4895. Lovely Dogs | Mathew_Miao | 0 | 9ms | 32940kb | C++23 | 4.1kb | 2024-07-11 20:26:33 | 2024-07-11 20:26:34 |
Judging History
answer
#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<string>
#include<bitset>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=2e5+10;
const int N=2e5;
const int INF=0x3f3f3f3f;
const long long LINF=0x3f3f3f3f3f3f3f3f;
inline int gcd(int a,int b){
if(!a){
return b;
}
int az=__builtin_ctz(a),bz=__builtin_ctz(b),z=(az>bz)?bz:az,t;
b>>=bz;
while(a)
{
a>>=az;
t=a-b;
az=__builtin_ctz(t);
b=a<b?a:b;a=t<0?-t:t;
}
return b<<z;
}
int n,d;
int cnt=0;
int prime[MAXN];
bool isp[MAXN];
int mob[MAXN],f[MAXN],g[MAXN];
int lim=0;
basic_string <int> fac[MAXN];
#define pii pair<int,int>
basic_string <pii> gfac[MAXN];
inline void init(){
memset(isp,true,sizeof(int)*(n+1));
isp[0]=false;
isp[1]=false;
mob[1]=1;
g[1]=1;
for(int i=2;i<=n;i++)
{
if(isp[i]){
cnt++;
prime[cnt]=i;
mob[i]=-1;
g[i]=-1;
}
for(int j=1;j<=cnt&&i*prime[j]<=n;j++)
{
isp[i*prime[j]]=false;
if(i%prime[j]==0){
mob[i*prime[j]]=0;
g[i*prime[j]]=-g[i];
break;
}
mob[i*prime[j]]=-mob[i];
g[i*prime[j]]=-g[i];
}
}
for(int i=1;i<=n;i++)
{
f[i]=g[i];
for(int j=i;j<=n;j+=i)
{
fac[j].push_back(i);
}
}
for(int i=1;i<=n;i++)
{
long long now=1;
for(int j=1;j<=d+1;j++)
{
now=now*i;
}
if(now>n*n){
break;
}
if(now>1&&now<=n){
for(int j=now;j<=n;j+=now)
{
f[j]=0;
}
}
for(int j=i;j<=n;j+=i)
{
long long val=now/gcd(now%j,j);
if(val<=n&&mob[j]){
gfac[j].push_back(pii(i,(int)val));
}
}
}
}
int dad[MAXN];
basic_string <int> tr[MAXN];
inline void add_edge(int x,int y){
tr[x].push_back(y);
}
int dfc=0;
int siz[MAXN],son[MAXN];
int dfn[MAXN],back[MAXN];
void Dfs(int x){
dfc++;
dfn[x]=dfc;
back[dfc]=x;
siz[x]=1;
for(int to:tr[x])
{
if(to==dad[x]){
continue;
}
dad[to]=x;
Dfs(to);
siz[x]+=siz[to];
if(siz[to]>siz[son[x]]){
son[x]=to;
}
}
}
int ans[MAXN],s[MAXN];
bool vis[MAXN];
basic_string <int> upd;
inline void modify(int x){
if(!f[x]){
return ;
}
for(int i:fac[x])
{
s[i]+=f[x];
if(!vis[i]){
vis[i]=true;
upd.push_back(i);
}
}
}
inline int query(int x){
int res=0;
for(auto [t,Gcd]:gfac[x])
{
res+=mob[t]*s[Gcd];
}
return res*g[x];
}
void dfs(int x){
for(int to:tr[x])
{
if(to==dad[x]||to==son[x]){
continue;
}
dfs(to);
for(int x:upd)
{
s[x]=0;
vis[x]=false;
}
}
if(son[x]){
dfs(son[x]);
ans[x]=ans[son[x]];
}
modify(x);
ans[x]+=query(x);
for(int to:tr[x])
{
if(to==dad[x]||to==son[x]){
continue;
}
for(int i=dfn[to];i<dfn[to]+siz[to];i++)
{
int now=back[i];
modify(now);
ans[x]+=query(now);
}
}
}
int rt;
int a[MAXN];
int u[MAXN],v[MAXN];
signed main(){
scanf("%d%d",&n,&d);
init();
for(int i=1;i<n;i++)
{
scanf("%d%d",&u[i],&v[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<n;i++)
{
add_edge(a[u[i]],a[v[i]]);
add_edge(a[v[i]],a[u[i]]);
}
rt=a[1];
Dfs(rt);
dfs(rt);
for(int i=1;i<=n;i++)
{
printf("%d\n",ans[a[i]]);
}
return 0;
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 32620kb
input:
20 2 18 8 18 11 13 19 10 8 9 11 4 8 9 15 9 17 2 1 13 18 20 18 1 8 12 17 7 16 5 11 16 15 6 19 14 16 1 3 2 15 5 13 20 6 16 18 9 19 17 7 14 10 11 3 1 12 4 8
output:
13 1 1 1 0 1 0 9 3 1 4 1 3 1 2 1 1 4 1 0
result:
wrong answer 1st words differ - expected: '16', found: '13'
Subtask #2:
score: 0
Wrong Answer
Test #24:
score: 10
Accepted
time: 4ms
memory: 30780kb
input:
2000 1 134 1468 867 1750 351 1220 1690 1888 1685 134 585 282 1142 643 206 271 260 1833 1987 770 1029 1667 322 1371 341 518 601 915 119 893 1933 1502 951 1785 1056 1630 1957 1208 96 55 1508 1212 331 427 505 151 1378 1486 1545 697 1459 629 202 997 180 1917 1638 1177 1244 1896 302 658 1433 1605 1318 19...
output:
581 -3 0 0 0 0 0 0 0 -2 0 0 0 0 0 -1 0 0 0 1 0 -1 0 0 -1 0 0 0 17 -2 0 -1 -2 0 0 0 0 0 0 0 -5 0 0 0 0 -14 0 -1 0 -1 0 0 1 1 -1 -4 0 0 1 0 0 0 3 0 0 0 -1 -2 0 0 4 0 0 0 0 -1 0 1 0 0 0 -5 0 0 0 0 -1 0 0 0 0 0 0 0 1 -1 0 18 0 0 13 -2 0 -2 0 0 0 0 2 -2 2 0 0 3 0 -1 0 0 0 0 -3 0 0 0 0 0 0 1 -1 0 0 0 0 0 ...
result:
ok 2000 tokens
Test #25:
score: 0
Accepted
time: 4ms
memory: 30696kb
input:
2000 1 1754 1650 906 642 596 1542 1656 1549 716 1578 1799 1182 53 244 1032 41 1290 1758 485 1496 1438 948 1683 684 400 653 1756 1459 1965 1322 1540 1263 1365 1564 108 1801 741 717 1113 13 1787 1124 411 732 64 1817 907 259 1308 29 1518 752 375 422 663 1631 528 799 863 310 790 793 587 579 1828 874 502...
output:
581 0 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 -12 0 0 0 0 0 0 2 -1 0 0 0 0 0 0 0 0 0 -1 -1 1 0 0 -2 0 20 -1 0 -3 1 0 5 -1 0 0 1 1 0 0 0 0 -1 0 -2 0 0 0 -15 1 1 0 0 0 0 0 1 0 30 0 1 0 0 1 -1 0 0 0 0 -1 0 0 0 0 0 1 0 0 0 0 0 0 0 -1 0 1 0 1 -1 0 -1 0 0 0 0 -3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 1 0 0 0...
result:
ok 2000 tokens
Test #26:
score: 0
Accepted
time: 9ms
memory: 32940kb
input:
2000 1 146 1160 146 388 146 1033 382 1917 162 1342 1 1425 1841 764 1674 780 1109 1649 1282 1786 488 1386 1753 1698 17 192 1692 944 693 146 1933 146 976 463 1603 392 1709 248 18 678 146 1157 1517 1416 31 1153 973 39 1359 1046 625 1840 745 146 1316 146 124 146 627 1410 146 540 772 1461 1041 1537 1374 ...
output:
581 0 -36 -192 435 473 506 0 0 358 0 0 0 0 0 0 -180 35 438 0 0 0 607 0 0 0 -17 0 0 26 2 499 0 -180 -85 0 -104 -120 -63 -60 0 0 0 0 598 0 0 0 0 356 592 0 0 0 45 -73 -116 343 0 0 0 0 0 0 31 -108 -5 0 0 0 637 270 494 0 0 487 0 -197 0 401 520 0 0 52 0 128 0 0 82 0 0 0 -22 0 0 0 0 0 0 30 0 0 0 0 0 0 0 0 ...
result:
ok 2000 tokens
Test #27:
score: 0
Accepted
time: 3ms
memory: 30812kb
input:
2000 1 681 278 1551 1142 424 928 738 174 1393 1727 456 944 1713 468 359 1597 1265 1737 246 500 1095 695 654 904 1465 27 1172 1385 1455 40 1391 1384 1979 970 1123 800 1618 1892 1444 1506 79 806 313 1350 1872 85 1467 1031 741 1139 739 1681 263 1454 169 885 1222 153 864 799 192 1339 935 1843 1633 1358 ...
output:
581 0 -1 0 0 0 1 25 0 0 0 0 1 0 -8 0 0 -3 0 0 0 -2 0 0 -3 0 0 0 0 0 0 0 -3 -1 -1 0 0 -1 1 -2 3 0 0 0 0 0 2 3 0 0 -1 -7 0 0 0 0 0 -7 0 0 -1 1 9 0 0 -1 0 0 0 0 0 -1 -3 -1 1 -3 0 -1 -1 0 -1 -1 -1 -1 0 0 -2 -1 12 -7 -10 0 0 0 -6 0 0 0 0 41 0 0 -15 0 0 0 0 0 1 1 0 0 0 0 0 -7 0 -3 -26 0 0 0 0 0 0 0 0 0 2 ...
result:
ok 2000 tokens
Test #28:
score: -10
Wrong Answer
time: 9ms
memory: 32764kb
input:
2000 2 1608 842 1808 1921 1404 549 594 1521 1755 855 1047 1256 340 1877 407 670 1100 1239 1511 1142 790 1103 1212 944 515 167 180 415 399 1563 1458 136 728 1480 1074 819 555 1594 1693 1301 1802 1879 1936 501 306 87 1125 796 720 1298 1999 1529 767 1396 1258 1940 1651 1564 1059 281 704 848 1861 473 13...
output:
789 3 4 6 1 1 1 1 17 1 1 1 1 0 5 3 7 1 0 1 0 2 1 1 1 0 3 6 0 0 1 109 0 2 9 50 0 3 0 0 1 1 1 1 1 1 1 7 1 0 7 1 1 2 0 0 0 0 1 0 1 0 4 1 1 3 1 1 1 1 1 1 1 1 0 2 0 1 1 1 6 2 1 1 0 0 3 1 0 1 1 1 3 1 1 0 1 4 0 1 1 1 0 1 1 1 3 0 0 0 1 1 0 1 1 1 1 0 1 1 27 1 0 1 1 0 1 1 2 0 1 4 1 3 0 2 1 1 0 2 3 1 1 0 1 1 2...
result:
wrong answer 1st words differ - expected: '2854', found: '789'
Subtask #3:
score: 0
Runtime Error
Test #45:
score: 0
Runtime Error
input:
200000 20 117994 12616 53490 106425 103660 50033 132640 78252 58384 19939 69183 10015 39098 165030 179856 130356 65245 57831 18234 83378 4240 154896 177149 102260 4634 180087 132390 19627 98506 60775 1890 120740 87908 21917 41323 192721 181885 96684 69412 139951 9800 38301 59025 29879 186185 81402 1...
output:
result:
Subtask #4:
score: 0
Runtime Error
Test #50:
score: 0
Runtime Error
input:
200000 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 61...
output:
result:
Subtask #5:
score: 0
Runtime Error
Test #55:
score: 0
Runtime Error
input:
200000 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 61...
output:
result:
Subtask #6:
score: 0
Time Limit Exceeded
Test #78:
score: 0
Time Limit Exceeded
input:
50000 1 8097 41839 17674 41774 40520 8024 5786 38261 20664 43471 1217 49276 11185 40807 14186 25584 31704 14814 42333 41475 13053 39565 45938 30104 5826 39463 5031 10814 43784 6042 58 33849 42978 18978 36307 33276 34769 4351 27884 37532 27528 29431 29451 39345 10946 9667 19016 47269 7911 30103 10308...
output:
result:
Subtask #7:
score: 0
Runtime Error
Test #103:
score: 0
Runtime Error
input:
200000 1 118863 188865 188022 168616 118976 119404 178852 33449 81624 40431 151228 160976 68943 136313 57200 117631 147789 139875 100240 55537 164811 145415 103548 186750 15010 168029 155731 107005 69836 1502 86171 122700 83448 131948 189162 94464 128210 2509 49724 183329 174782 192641 27687 71315 1...