結果

問題 No.2360 Path to Integer
ユーザー 👑 kmjpkmjp
提出日時 2023-06-23 23:08:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 94 ms / 2,500 ms
コード長 1,847 bytes
コンパイル時間 3,272 ms
コンパイル使用メモリ 202,656 KB
実行使用メモリ 18,156 KB
最終ジャッジ日時 2023-09-13 18:16:41
合計ジャッジ時間 4,273 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,464 KB
testcase_01 AC 4 ms
8,496 KB
testcase_02 AC 3 ms
8,440 KB
testcase_03 AC 3 ms
8,328 KB
testcase_04 AC 4 ms
8,332 KB
testcase_05 AC 3 ms
8,500 KB
testcase_06 AC 3 ms
8,304 KB
testcase_07 AC 4 ms
8,424 KB
testcase_08 AC 10 ms
8,884 KB
testcase_09 AC 94 ms
12,880 KB
testcase_10 AC 70 ms
12,884 KB
testcase_11 AC 73 ms
12,812 KB
testcase_12 AC 66 ms
12,620 KB
testcase_13 AC 91 ms
12,844 KB
testcase_14 AC 90 ms
17,264 KB
testcase_15 AC 94 ms
12,824 KB
testcase_16 AC 90 ms
18,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N;
ll A[1010101],D[101010];
vector<int> E[101010];
const ll mo=998244353;

int NV[101010];
ll dp[101010];
ll ret;
ll p10[11];
ll modpow(ll a, ll n = mo-2) {
	ll r=1;a%=mo;
	while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
	return r;
}

void dfs(int cur,int pre) {
	dp[cur]=1;
	NV[cur]=1;
	FORR(e,E[cur]) if(e!=pre) {
		dfs(e,cur);
		dp[cur]+=dp[e];
		NV[cur]+=NV[e];
	}
	dp[cur]=dp[cur]%mo*p10[D[cur]]%mo;
	return;
}

void dfs2(int cur,int pre,ll par) {
	dp[cur]=dp[cur]*modpow(p10[D[cur]])%mo;
	(ret+=(N-NV[cur])*dp[cur]%mo*A[cur])%=mo;
	(dp[cur]+=par)%=mo;
	(ret+=dp[cur]%mo*A[cur])%=mo;
	
	FORR(e,E[cur]) if(e!=pre) {
		ll d=(dp[cur]-dp[e]+mo)*p10[D[cur]]%mo;
		(ret+=(dp[cur]-dp[e])*(NV[e])%mo*A[cur])%=mo;
		dfs2(e,cur,d);
	}
	
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	p10[0]=1;
	for(i=1;i<=10;i++) p10[i]=p10[i-1]*10%mo;
	
	cin>>N;
	FOR(i,N) {
		cin>>s;
		A[i]=atoll(s.c_str())%mo;
		D[i]=s.size();
	}
	FOR(i,N-1) {
		cin>>x>>y;
		E[x-1].push_back(y-1);
		E[y-1].push_back(x-1);
	}
	
	dfs(0,0);
	dfs2(0,0,0);
	
	cout<<ret<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0