QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#308343 | #4795. Taxi | KLPP# | TL | 1995ms | 54940kb | C++20 | 2.8kb | 2024-01-19 23:04:59 | 2024-01-19 23:05:00 |
Judging History
answer
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long int lld;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define trav(a,v) for(auto a:v)
vector<pair<int,lld> > nei[1000000];
int sz[1000000];
bool visited[1000000];
vector<pair<lld,lld> >V;
lld tot[1000000];
const lld INF=1e18;
const lld MOD=1e9+7;
const int MAX=3'000'000;
lld fact[MAX];
lld inv[MAX];
lld ModPow(lld base, lld exp){
if(exp==0)return 1;
if(exp%2==1)return (base*ModPow(base,exp-1))%MOD;
lld a=ModPow(base,exp/2);
return (a*a)%MOD;
}
lld comb(int a, int b){
if(b<0 || b>a)return 0;
lld ans=fact[a];
ans*=inv[b];
ans%=MOD;
ans*=inv[a-b];
ans%=MOD;
return ans;
}
lld GCD(lld a, lld b){
if(b==0)return a;
return GCD(b,a%b);
}
const int Mx2=6000;
lld prd[Mx2];
int n;
void DFS(int node){
visited[node]=true;
sz[node]=1;
trav(a,nei[node]){
if(!visited[a.first]){
DFS(a.first);
tot[min(sz[a.first],n-sz[a.first])]+=a.second;
sz[node]+=sz[a.first];
}
}
}
void solve(){
int m;
cin>>n>>m;
rep(i,0,n-1){
int x,y;
lld l;
cin>>x>>y>>l;
x--;y--;
nei[x].push_back({y,l});
nei[y].push_back({x,l});
}
rep(i,0,n+1)tot[i]=0;
DFS(0);
lld ans=0;
rep(i,1,n){
lld conj=n-i;
lld can=1;
lld tot2=0;
rep(c,0,2*m+1){
lld par=min(c,2*m-c);
lld tot3=ModPow(i,c);
tot3*=ModPow(conj,2*m-c);
tot3%=MOD;
tot3*=comb(2*m,c);
tot3%=MOD;
//~ rep(a,0,c+1){
//~ lld b=c-a;
//~ if(0<=a && a<=m && 0<=b && b<=m){
//~ }else continue;
//~ lld par2=ModPow(i,a)*ModPow(i,b);
//~ par2%=MOD;
//~ par2*=ModPow(conj,m-a);
//~ par2%=MOD;
//~ par2*=comb(m,a);
//~ par2%=MOD;
//~ par2*=ModPow(conj,m-b);
//~ par2%=MOD;
//~ par2*=comb(m,b);
//~ par2%=MOD;
//~ tot3+=par2;
//~ tot3%=MOD;
//~ }
//cout<<tot3<<" A "<<c<<" "<<i<<endl;
tot2+=tot3*par;
tot2%=MOD;
}
//cout<<i<<" "<<tot2<<"\n";
can*=tot2;
can%=MOD;
ans+=can*tot[i];
ans%=MOD;
}
cout<<ans<<"\n";
}
int main(){
fact[0]=1;
rep(i,1,MAX)fact[i]=(i*fact[i-1])%MOD;
inv[MAX-1]=ModPow(fact[MAX-1],MOD-2);
for(int i=MAX-2;i>-1;i--)inv[i]=((i+1)*inv[i+1])%MOD;
//~ rep(i,0,Mx2){
//~ rep(j,0,Mx2){
//~ if(j==0)Q[i][j]=1;
//~ else{
//~ if(i==0)Q[i][j]=0;
//~ else{
//~ Q[i][j]=(Q[i-1][j]+j*Q[i][j-1]);
//~ Q[i][j]%=MOD;
//~ }
//~ }
//~ Q[i][j]=ModPow(i,j)*(j+1);
//~ Q[i][j]%=MOD;
//~ }
//~ }
ios::sync_with_stdio(0);
cin.tie(0);
int tt=1;
//cin>>tt;
while(tt--){
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1995ms
memory: 54940kb
input:
2500 2500 531 1977 7285 705 44 4544 1409 2220 8896 2175 2086 9343 1947 729 2482 1114 1832 186 2388 1775 9841 1028 2097 7734 1949 2130 1115 407 1801 352 1165 254 8847 625 844 9853 551 1980 457 1683 163 7785 864 2338 1532 1214 1578 2055 387 1525 797 2070 1976 910 248 568 1444 2434 509 7641 2100 1779 6...
output:
796272069
result:
ok 1 number(s): "796272069"
Test #2:
score: 0
Accepted
time: 1995ms
memory: 54024kb
input:
2500 2500 2197 1252 7783 1344 1657 7880 354 952 8879 198 1840 820 1670 89 6814 931 1957 770 1629 1704 5668 1185 2264 1012 2429 1406 1478 1039 858 5411 363 430 2690 159 2155 2278 908 65 4608 867 955 3036 530 921 8958 1463 888 192 2059 2 3860 2045 1325 7513 1203 895 8310 853 1039 1229 1132 222 1274 20...
output:
800277337
result:
ok 1 number(s): "800277337"
Test #3:
score: -100
Time Limit Exceeded
input:
2500 2500 680 1473 8535 1796 255 283 1693 1500 1563 1499 2196 9824 371 1896 8832 899 1757 9220 1624 2246 1029 1081 195 1002 2409 1418 7220 1079 1571 7057 1846 2257 2286 259 739 9842 449 1797 4290 1457 1609 8548 803 184 7605 17 2427 2456 588 324 6053 1845 2070 8862 2470 1778 405 365 1239 207 2299 806...
output:
547423329