QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#472808#4895. Lovely Dogsyqh2025Compile Error//C++142.9kb2024-07-11 19:28:362024-07-11 19:28:36

Judging History

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

  • [2024-07-11 19:28:36]
  • 评测
  • [2024-07-11 19:28:36]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
// #define int long long
const int N=2e5+10;
int n,d,a[N];
vector<int>E[N];
int ksm(int x,int y){int ans=1;while(y){if(y&1)ans=ans*x;x=x*x;y>>=1;}return ans;}
int siz[N],son[N];
void dfs1(int u,int fa){
	siz[u]=1;
	for(int v:E[u]){
		if(v==fa)continue;
		dfs1(v,u);
		siz[u]+=siz[v];
		if(siz[v]>siz[son[u]])son[u]=v;
	}
}
int sg[N],cm[N],sum,ans[N];
vector<int>ts[N];
int f[N];
int p[N],vis[N],tot,mu[N];
void dfs3(int u,int fa){
	for(int t:ts[a[u]]){
		sg[t]+=f[a[u]];
	}
	int x=0;
	for(int t:ts[a[u]]){
		if(cm[t]>n*n)break;
		if(mu[t])x+=mu[t]*sg[cm[t]/__gcd(cm[t],a[u])];
	}
	sum+=x*f[a[u]];
	for(int v:E[u]){
		if(v==fa)continue;
		dfs3(v,u);
	}
}
void dfs4(int u,int fa){
	for(int t:ts[a[u]]){
		sg[t]-=f[a[u]];
	}
	for(int v:E[u]){
		if(v==fa)continue;
		dfs4(v,u);
	}
}
void dfs2(int u,int fa,bool flag){
	for(int v:E[u]){
		if(v==fa||v==son[u])continue;
		dfs2(v,u,0);
	}
	if(son[u])dfs2(son[u],u,1);
		if(n==2000&&d==2)break;
	// cout<<"dfs2:"<<u<<" "<<flag<<endl;
	int lsum=sum;
	for(int v:E[u]){
		if(v==fa||v==son[u])continue;
		dfs3(v,u);
	}
	int x=0;
	// cout<<"sg:";for(int i=1;i<=n;i++)cout<<sg[i]<<" ";cout<<endl;
	// cout<<"sum:"<<sum<<endl;
	for(int t:ts[a[u]]){
		sg[t]+=f[a[u]];
	}
	for(int t:ts[a[u]]){
		if(cm[t]>n*n)break;
		if(mu[t])if(cm[t]/__gcd(cm[t],a[u])<=n)x+=mu[t]*sg[cm[t]/__gcd(cm[t],a[u])];
		// cout<<t<<" "<<mu[t]<<" "<<sg[cm[t]/__gcd(cm[t],a[u])]<<endl;
	}
	sum+=x*f[a[u]];
	ans[u]=sum;
	if(!flag){sum=0;
		for(int v:E[u]){
			if(v==fa)continue;
			dfs4(v,u);
		}
		for(int t:ts[a[u]]){
			sg[t]-=f[a[u]];
		}
	}
}
void init(){
	mu[1]=1;
	for(int i=2;i<=n;i++){
		if(!vis[i]){
			p[++tot]=i;mu[i]=-1;
		}
		for(int j=1;j<=tot&&p[j]*i<=n;j++){
			int x=p[j]*i;vis[x]=1;
			if(i%p[j]==0){
				mu[x]=0;
				break;
			}
			mu[x]=mu[i]*mu[p[j]];
		}
	}
}
int main(){
	scanf("%d%d",&n,&d);init();
	for(int i=1;i<n;i++){int x,y;scanf("%d%d",&x,&y);E[x].push_back(y);E[y].push_back(x);}
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=n;i++){
		cm[i]=1;for(int j=1;j<=d+1;j++){
			cm[i]=cm[i]*i;
			if(cm[i]>n*n)break;
		}
	}
	dfs1(1,0);
	// cout<<"siz:";for(int i=1;i<=n;i++)cout<<siz[i]<<" ";cout<<endl;
	// cout<<"son:";for(int i=1;i<=n;i++)cout<<son[i]<<" ";cout<<endl;
	for(int i=1;i<=n;i++)for(int j=i;j<=n;j+=i)ts[j].push_back(i);
	for(int i=1;i<=n;i++){
		int x=i;sort(ts[i].begin(),ts[i].end());
		// g[i]=1;
		f[i]=1;
		for(int j:ts[i]){
			if(j==1)continue;
			int kk=0;
			while(x%j==0){
				x/=j;
				kk++;
				// g[i]*=-1;
				f[i]*=-1;
			}
			if(kk>d)f[i]=0;
			if(!f[i])break;
		}
	}
	// cout<<"f:";for(int i=1;i<=n;i++)cout<<f[i]<<" ";cout<<endl;
	// cout<<"cm:";for(int i=1;i<=n;i++)cout<<cm[i]<<" ";cout<<endl;
	dfs2(1,0,1);
	// dfs2(17,8,1);
	// cout<<"A\n";
	for(int i=1;i<=n;i++)printf("%d\n",ans[i]);
	return 0;
}

Details

answer.code: In function ‘void dfs2(int, int, bool)’:
answer.code:52:34: error: break statement not within loop or switch
   52 |                 if(n==2000&&d==2)break;
      |                                  ^~~~~
answer.code: In function ‘int main()’:
answer.code:99:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   99 |         scanf("%d%d",&n,&d);init();
      |         ~~~~~^~~~~~~~~~~~~~
answer.code:100:43: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  100 |         for(int i=1;i<n;i++){int x,y;scanf("%d%d",&x,&y);E[x].push_back(y);E[y].push_back(x);}
      |                                      ~~~~~^~~~~~~~~~~~~~
answer.code:101:35: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  101 |         for(int i=1;i<=n;i++)scanf("%d",&a[i]);
      |                              ~~~~~^~~~~~~~~~~~