結果
| 問題 |
No.1784 Not a star yet...
|
| コンテスト | |
| ユーザー |
sigma425
|
| 提出日時 | 2021-12-14 23:55:31 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 83 ms / 2,000 ms |
| コード長 | 11,568 bytes |
| コンパイル時間 | 4,530 ms |
| コンパイル使用メモリ | 216,400 KB |
| 最終ジャッジ日時 | 2025-01-26 22:37:52 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 61 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb push_back
#define eb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> bool chmax(T& x, U y){
if(x<y){ x=y; return true; }
return false;
}
template<class T,class U> bool chmin(T& x, U y){
if(y<x){ x=y; return true; }
return false;
}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}
template<class T>
V<T> Vec(size_t a) {
return V<T>(a);
}
template<class T, class... Ts>
auto Vec(size_t a, Ts... ts) {
return V<decltype(Vec<T>(ts...))>(a, Vec<T>(ts...));
}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
o<<"{";
for(const T& v:vc) o<<v<<",";
o<<"}";
return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }
#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ~ ";
dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \
for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif
template<class D> D divFloor(D a, D b){
return a / b - (((a ^ b) < 0 && a % b != 0) ? 1 : 0);
}
template<class D> D divCeil(D a, D b) {
return a / b + (((a ^ b) > 0 && a % b != 0) ? 1 : 0);
}
template<unsigned int mod_>
struct ModInt{
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr static uint mod = mod_;
uint v;
ModInt():v(0){}
ModInt(ll _v):v(normS(_v%mod+mod)){}
explicit operator bool() const {return v!=0;}
static uint normS(const uint &x){return (x<mod)?x:x-mod;} // [0 , 2*mod-1] -> [0 , mod-1]
static ModInt make(const uint &x){ModInt m; m.v=x; return m;}
ModInt operator+(const ModInt& b) const { return make(normS(v+b.v));}
ModInt operator-(const ModInt& b) const { return make(normS(v+mod-b.v));}
ModInt operator-() const { return make(normS(mod-v)); }
ModInt operator*(const ModInt& b) const { return make((ull)v*b.v%mod);}
ModInt operator/(const ModInt& b) const { return *this*b.inv();}
ModInt& operator+=(const ModInt& b){ return *this=*this+b;}
ModInt& operator-=(const ModInt& b){ return *this=*this-b;}
ModInt& operator*=(const ModInt& b){ return *this=*this*b;}
ModInt& operator/=(const ModInt& b){ return *this=*this/b;}
ModInt& operator++(int){ return *this=*this+1;}
ModInt& operator--(int){ return *this=*this-1;}
template<class T> friend ModInt operator+(T a, const ModInt& b){ return (ModInt(a) += b);}
template<class T> friend ModInt operator-(T a, const ModInt& b){ return (ModInt(a) -= b);}
template<class T> friend ModInt operator*(T a, const ModInt& b){ return (ModInt(a) *= b);}
template<class T> friend ModInt operator/(T a, const ModInt& b){ return (ModInt(a) /= b);}
ModInt pow(ll p) const {
if(p<0) return inv().pow(-p);
ModInt a = 1;
ModInt x = *this;
while(p){
if(p&1) a *= x;
x *= x;
p >>= 1;
}
return a;
}
ModInt inv() const { // should be prime
return pow(mod-2);
}
// ll extgcd(ll a,ll b,ll &x,ll &y) const{
// ll p[]={a,1,0},q[]={b,0,1};
// while(*q){
// ll t=*p/ *q;
// rep(i,3) swap(p[i]-=t*q[i],q[i]);
// }
// if(p[0]<0) rep(i,3) p[i]=-p[i];
// x=p[1],y=p[2];
// return p[0];
// }
// ModInt inv() const {
// ll x,y;
// extgcd(v,mod,x,y);
// return make(normS(x+mod));
// }
bool operator==(const ModInt& b) const { return v==b.v;}
bool operator!=(const ModInt& b) const { return v!=b.v;}
bool operator<(const ModInt& b) const { return v<b.v;}
friend istream& operator>>(istream &o,ModInt& x){
ll tmp;
o>>tmp;
x=ModInt(tmp);
return o;
}
friend ostream& operator<<(ostream &o,const ModInt& x){ return o<<x.v;}
};
int bsf(ll x) { return __builtin_ctzll(x); }
ll gcd(ll a, ll b){
a = abs(a), b = abs(b);
if(a==0) return b;
if(b==0) return a;
int shift = bsf(a|b);
a >>= bsf(a);
do{
b >>= bsf(b);
if(a>b) swap(a,b);
b -= a;
}while(b);
return a<<shift;
}
struct Frac{
ll x,y; // x/y
Frac(ll x_ = 0):x(x_),y(1){}
Frac(ll x_,ll y_){
ll g = gcd(x_,y_);
if(y_ < 0) g = -g;
x = x_ / g;
y = y_ / g;
}
Frac operator-() const { return {-x,y}; }
Frac operator+(const Frac& r) const { return {x * r.y + y * r.x, y * r.y}; }
Frac operator-(const Frac& r) const { return *this + (-r); }
Frac operator*(const Frac& r) const { return {x * r.x, y * r.y}; }
Frac operator/(const Frac& r) const { return {x * r.y, y * r.x}; }
Frac& operator+=(const Frac& r) { return *this = *this + r; }
Frac& operator-=(const Frac& r) { return *this = *this - r; }
Frac& operator*=(const Frac& r) { return *this = *this * r; }
Frac& operator/=(const Frac& r) { return *this = *this / r; }
template<class T> friend Frac operator+(T a, const Frac& b){ return (Frac(a) += b);}
template<class T> friend Frac operator-(T a, const Frac& b){ return (Frac(a) -= b);}
template<class T> friend Frac operator*(T a, const Frac& b){ return (Frac(a) *= b);}
template<class T> friend Frac operator/(T a, const Frac& b){ return (Frac(a) /= b);}
bool operator<(const Frac& r) const { return x * r.y < y * r.x; }
bool operator>(const Frac& r) const { return r < *this; }
bool operator<=(const Frac& r) const { return !(r < *this); }
bool operator>=(const Frac& r) const { return !(*this < r); }
bool operator==(const Frac& r) const { return x == r.x && y == r.y; }
bool operator!=(const Frac& r) const { return !(*this == r); }
Frac inv() const {
return Frac(y,x);
}
friend ostream& operator<<(ostream &o,const Frac& x){
return o << x.x << "/" << x.y;
}
};
using mint = ModInt<998244353>;
// using mint = Frac;
V<mint> operator+(const V<mint>& a, const V<mint>& b){
V<mint> c = a;
rep(i,si(b)) c[i] += b[i];
return c;
}
V<mint> operator*(const V<mint>& a, mint v){
V<mint> c = a;
rep(i,si(a)) c[i] *= v;
return c;
}
template<class T>
struct Matrix{
int H,W;
VV<T> a;
Matrix() : H(0),W(0){}
Matrix(int H_,int W_) : H(H_),W(W_),a( VV<T>(H,V<T>(W)) ){}
Matrix(const VV<T>& v) : H(v.size()), W(v[0].size()), a(v){}
static Matrix E(int n){
Matrix a(n,n);
rep(i,n) a[i][i] = 1;
return a;
}
V<T>& operator[](int i){return a[i];}
const V<T>& operator[](int i) const {return a[i];}
Matrix operator+(const Matrix& r) const {
assert(H==r.H && W==r.W);
VV<T> v(H,V<T>(W));
rep(i,H) rep(j,W) v[i][j] = a[i][j] + r.a[i][j];
return Matrix(v);
}
Matrix operator-(const Matrix& r) const {
assert(H==r.H && W==r.W);
VV<T> v(H,V<T>(W));
rep(i,H) rep(j,W) v[i][j] = a[i][j] - r.a[i][j];
return Matrix(v);
}
Matrix operator*(const Matrix& r) const {
assert(W==r.H);
VV<T> v(H,V<T>(r.W));
rep(i,H) rep(k,W) rep(j,r.W) v[i][j] += a[i][k] * r.a[k][j];
return Matrix(v);
}
Matrix& operator+=(const Matrix& r){return (*this)=(*this)+r;}
Matrix& operator-=(const Matrix& r){return (*this)=(*this)-r;}
Matrix& operator*=(const Matrix& r){return (*this)=(*this)*r;}
Matrix pow(ll p) const {
assert(H == W);
Matrix res = E(H);
Matrix x = *this;
while(p){
if(p&1) res *= x;
x *= x;
p >>= 1;
}
return res;
}
friend ostream& operator<<(ostream &o,const Matrix& A){
rep(i,A.H){
rep(j,A.W) o<<A.a[i][j]<<" ";
o<<endl;
}
return o;
}
/*
副作用がある, 基本的に自分でこれを呼ぶことはない
掃き出し法をする
左からvar列が掃き出す対象で、それより右は同時に値を変更するだけ(e.g. 逆行列は右に単位行列おいてから掃き出す)
行swap, 列swap は行わない
rank を返す
*/
int sweep(int var){
int rank = 0;
vector<bool> used(H);
rep(j,var){
int i=0;
while(i<H && (used[i]||iszero(a[i][j]))) i++;
if(i==H) continue;
used[i] = true;
rank++;
T t = a[i][j];
rep(k,W) a[i][k] = a[i][k]/t;
rep(k,H) if(k!=i){
T tt = a[k][j];
rep(l,W) a[k][l] = a[k][l]-a[i][l]*tt;
}
}
return rank;
}
};
bool iszero(mint x){return x==0;}
bool isone(mint x){return x==1;}
template<class T>
pair< int, vector<T> > solveLinearEquation(const Matrix<T>& A, vector<T> b){
assert(A.H==(int)b.size());
int H = A.H, W = A.W;
Matrix<T> X(H,W+1);
rep(i,H) rep(j,W) X[i][j] = A[i][j];
rep(i,H) X[i][W] = b[i];
int rank = X.sweep(W);
rep(i,H){
bool allzero = true;
rep(j,W) if(!iszero(X[i][j])) allzero = false;
if(allzero){
if(!iszero(X[i][W])){ //0x + 0y + 0z = non0
return pair<int,vector<T> >(-1,vector<T>());
}
}
}
vector<bool> done(H);
vector<T> x(W);
rep(j,W){
int c0 = 0, c1 = 0;
int I = -1;
rep(i,H){
if(iszero(X[i][j])) c0++;
else if(isone(X[i][j])) c1++,I=i;
}
if(c0==H-1 && c1==1 && !done[I]){
x[j] = X[I][W];
done[I] = true;
}
}
return pair<int,vector<T> >(W-rank,x);
}
int X,Y;
V<mint> f[255][255];
V<mint> F(int x,int y){
if(0<=x&&x<=X&&0<=y&&y<=Y) return f[x][y];
return V<mint>(Y+2,0);
}
V<mint> Const(mint v){
V<mint> res(Y+2); res[Y+1] = v;
return res;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false); //DON'T USE scanf/printf/puts !!
cout << fixed << setprecision(20);
int N; cin >> N;
V<int> sx(N),sy(N);
rep(i,N-1){
int a,b,c; cin >> a >> b >> c; a--,b--;
if(c == 1){
sx[a]++,sx[b]++; X++;
}else{
sy[a]++,sy[b]++; Y++;
}
}
int fr = N*(N-1)/2 - (N-2);
rep(y,Y+1){
f[0][y] = V<mint>(Y+2); f[0][y][y] = 1;
}
auto A = Vec<mint>(Y+1,Y+1);
V<mint> b(Y+1);
rep(x,X+1) rep(y,Y+1){
mint U = x ? mint(x)/(X+2*Y) * (1 - mint(N-x-y)/fr) : 0;
mint D = x != X ? (mint(X-x)/(X+2*Y)) * (mint(N-1-x-y)/fr) : 0;
mint L = y ? mint(2*y)/(X+2*Y) * (1 - mint(N-x-y)/fr) : 0;
mint R = y != Y ? (mint(2*(Y-y))/(X+2*Y)) * (mint(N-1-x-y)/fr) : 0;
mint M = 1-U-D-L-R;
show("----------------");
shows(x,y);
show(U);show(D);show(L);show(R);show(M);
show("-----------------");
// f(x,y) = Uf(x-1,y) + Df(x+1,y) + Lf(x,y-1) + Rf(x,y+1) + Mf(x,y) + 1/N
if(x != X){
// compute f(x+1,y)
f[x+1][y] = ( F(x-1,y) * U + F(x,y-1) * L + F(x,y+1) * R + F(x,y) * (M-1) + Const(mint(N).inv()) ) * (-D).inv();
}else{
if(y != Y){
// this should be 0
auto exp = F(x-1,y) * U + F(x+1,y) * D + F(x,y-1) * L + F(x,y+1) * R + F(x,y) * (M-1) + Const(mint(N).inv());
rep(i,Y+1) A[y][i] = exp[i];
b[y] = -exp[Y+1];
}else{
// f[X][Y] = const
rep(i,Y+1) A[y][i] = f[X][Y][i];
b[y] = -f[X][Y][Y+1];
}
}
}
auto p = solveLinearEquation<mint>(A,b);
show(p.fs);
auto Eval = [&](V<mint> f){
mint res = 0;
rep(i,Y+1) res += f[i] * p.sc[i];
res += f[Y+1];
return res;
};
if(false){
shows("f_debug");
rep(x,X+1){
rep(y,Y+1) cout << Eval(f[x][y]) << " ";
cout << endl;
}
}
mint bg = 0, en = 0;
rep(i,N) bg += Eval(f[sx[i]][sy[i]]);
en = Eval(f[X][Y]) + Eval(F(1,0)*X) + Eval(F(0,1)*Y);
cout << bg-en << endl;
}
sigma425