結果

問題 No.1917 LCMST
コンテスト
ユーザー vjudge1
提出日時 2025-05-05 13:24:18
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 938 bytes
コンパイル時間 1,564 ms
コンパイル使用メモリ 160,272 KB
実行使用メモリ 45,680 KB
最終ジャッジ日時 2025-05-05 13:25:45
合計ジャッジ時間 18,508 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 3
other AC * 3 WA * 39
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:21:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   21 |                 scanf("%d",&t[i]);
      |                 ~~~~~^~~~~~~~~~~~

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,t[N],f[N];
inline int fa(int x){
	if(x==f[x])
		return x;
	return f[x]=fa(f[x]);
}
vector<int> a[N];
bool bj[N];
vector<int> q;
int main()
{
	//freopen("tree.in","r",stdin);
	//freopen("tree.out","w",stdout);
	cin>>n;
	int maxn=0;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&t[i]);
		a[t[i]].push_back(i);
		f[i]=i;
		maxn=max(maxn,t[i]);
	}
	long long ans=0;
	int cnt=0;
	for(int i=maxn;i>=1;i--)
	{
		for(int j=i;j<=maxn;j+=i)
		{
			for(int k=0;k<a[j].size();k++)
			{
				if(!bj[fa(a[j][k])])
				{
					bj[fa(a[j][k])]=1;
					q.push_back(fa(a[j][k]));
				}
			}
		//	a[j].clear();
		}
		if(q.size()==0) continue ;
		for(int j=0;j<q.size();j++)
			bj[fa(q[j])]=0;
		ans+=1ll*i*(q.size()-1);
		cnt+=q.size()-1;
		for(int j=1;j<q.size();j++)
			f[fa(q[j])]=f[fa(q[j-1])];
	//	cout<<i<<" "<<q.size()<<"\n";
		q.clear();
		if(cnt==n-1) break;
	}
	cout<<ans;
	return 0;
}
0