結果

問題 No.1333 Squared Sum
ユーザー kotatsugamekotatsugame
提出日時 2021-01-08 22:22:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 368 ms / 2,000 ms
コード長 2,875 bytes
コンパイル時間 707 ms
コンパイル使用メモリ 77,292 KB
実行使用メモリ 40,832 KB
最終ジャッジ日時 2024-04-28 03:41:43
合計ジャッジ時間 11,725 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,472 KB
testcase_01 AC 5 ms
9,472 KB
testcase_02 AC 5 ms
9,600 KB
testcase_03 AC 343 ms
16,768 KB
testcase_04 AC 341 ms
16,896 KB
testcase_05 AC 334 ms
16,896 KB
testcase_06 AC 337 ms
16,768 KB
testcase_07 AC 339 ms
16,768 KB
testcase_08 AC 331 ms
16,816 KB
testcase_09 AC 349 ms
16,768 KB
testcase_10 AC 343 ms
16,768 KB
testcase_11 AC 342 ms
16,896 KB
testcase_12 AC 333 ms
16,896 KB
testcase_13 AC 335 ms
40,832 KB
testcase_14 AC 365 ms
28,544 KB
testcase_15 AC 366 ms
37,888 KB
testcase_16 AC 5 ms
9,512 KB
testcase_17 AC 5 ms
9,472 KB
testcase_18 AC 5 ms
9,472 KB
testcase_19 AC 5 ms
9,600 KB
testcase_20 AC 5 ms
9,528 KB
testcase_21 AC 5 ms
9,472 KB
testcase_22 AC 5 ms
9,604 KB
testcase_23 AC 5 ms
9,472 KB
testcase_24 AC 5 ms
9,472 KB
testcase_25 AC 5 ms
9,600 KB
testcase_26 AC 358 ms
40,192 KB
testcase_27 AC 353 ms
29,184 KB
testcase_28 AC 368 ms
39,936 KB
testcase_29 AC 331 ms
40,832 KB
testcase_30 AC 128 ms
12,540 KB
testcase_31 AC 67 ms
11,264 KB
testcase_32 AC 194 ms
14,052 KB
testcase_33 AC 143 ms
13,056 KB
testcase_34 AC 261 ms
15,484 KB
testcase_35 AC 197 ms
14,080 KB
testcase_36 AC 112 ms
12,288 KB
testcase_37 AC 113 ms
12,400 KB
testcase_38 AC 133 ms
12,672 KB
testcase_39 AC 217 ms
14,584 KB
testcase_40 AC 274 ms
20,304 KB
testcase_41 AC 274 ms
20,304 KB
testcase_42 AC 282 ms
20,304 KB
testcase_43 AC 253 ms
20,304 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
a.cpp:31:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]

ソースコード

diff #

#line 1 "a.cpp"
#include<iostream>
#include<vector>
using namespace std;
#line 3 "/home/kotatsugame/library/math/modint.cpp"
#include<utility>
template<int m>
struct modint{
	unsigned int x;
	constexpr modint()noexcept:x(){}
	template<typename T>
	constexpr modint(T x_)noexcept:x((x_%=m)<0?x_+m:x_){}
	constexpr unsigned int val()const noexcept{return x;}
	constexpr modint&operator++()noexcept{if(++x==m)x=0;return*this;}
	constexpr modint&operator--()noexcept{if(x==0)x=m;--x;return*this;}
	constexpr modint operator++(int)noexcept{modint res=*this;++*this;return res;}
	constexpr modint operator--(int)noexcept{modint res=*this;--*this;return res;}
	constexpr modint&operator+=(const modint&a)noexcept{x+=a.x;if(x>=m)x-=m;return*this;}
	constexpr modint&operator-=(const modint&a)noexcept{if(x<a.x)x+=m;x-=a.x;return*this;}
	constexpr modint&operator*=(const modint&a)noexcept{x=(unsigned long long)x*a.x%m;return*this;}
	constexpr modint&operator/=(const modint&a)noexcept{return*this*=a.inv();}
	constexpr modint operator+()const noexcept{return*this;}
	constexpr modint operator-()const noexcept{return modint()-*this;}
	constexpr modint pow(long long n)const noexcept
	{
		if(n<0)return pow(-n).inv();
		modint x=*this,r=1;
		for(;n;x*=x,n>>=1)if(n&1)r*=x;
		return r;
	}
	constexpr modint inv()const noexcept
	{
		int s=x,t=m,x=1,u=0;
		while(t)
		{
			int k=s/t;
			s-=k*t;
			swap(s,t);
			x-=k*u;
			swap(x,u);
		}
		return modint(x);
	}
	friend constexpr modint operator+(const modint&a,const modint&b){return modint(a)+=b;}
	friend constexpr modint operator-(const modint&a,const modint&b){return modint(a)-=b;}
	friend constexpr modint operator*(const modint&a,const modint&b){return modint(a)*=b;}
	friend constexpr modint operator/(const modint&a,const modint&b){return modint(a)/=b;}
	friend constexpr bool operator==(const modint&a,const modint&b){return a.x==b.x;}
	friend constexpr bool operator!=(const modint&a,const modint&b){return a.x!=b.x;}
	friend ostream&operator<<(ostream&os,const modint&a){return os<<a.x;}
	friend istream&operator>>(istream&is,modint&a){long long v;is>>v;a=modint(v);return is;}
};
#line 5 "a.cpp"
using mint=modint<(int)1e9+7>;
int N;
vector<pair<int,mint> >G[2<<17];
mint ans=0;
pair<mint,mint>dfs(int u,int p,mint d)
{
	vector<pair<mint,mint> >A;
	mint ch=1,ds=0;
	for(pair<int,mint>q:G[u])if(q.first!=p)
	{
		pair<mint,mint>r=dfs(q.first,u,d+q.second);
		A.push_back(r);
		ch+=r.first;
		ds+=r.second;
	}
	ans+=d*d*(N-2);
	for(pair<mint,mint>q:A)
	{
		ans+=2*(ch-q.first)*q.first*d*d;
		ans-=4*(ch-q.first)*d*q.second;
	}
	ans+=2*(ch-1)*d*d;
	ans-=4*(ch-1)*d*d;
	ds+=d;
	return make_pair(ch,ds);
}
main()
{
	cin>>N;
	for(int i=1;i<N;i++)
	{
		int u,v;mint w;cin>>u>>v>>w;
		u--,v--;
		G[u].push_back(make_pair(v,w));
		G[v].push_back(make_pair(u,w));
	}
	mint ad=dfs(0,0,mint(0)).second;
	ans+=ad*ad;
	cout<<ans<<endl;
}
0