//biblioteczka z bignumami: //https://github.com/mareksom/acmlib/blob/master/code/blazej/bigNum.cpp #include <x86intrin.h> #pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags #pragma GCC target("tune=native") //Enable AVX #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx") #include<stdint.h> #include<bits/stdc++.h> using namespace std; typedef long long lld; #define For(i,s,a) for(lld i = (lld)s; i < (lld)a; ++i) #define FORD(i,s,a) for(long long i = s; i>=a; --i) #define REP(i,a) for(long long i = 0; i<a;++i) #define FOR(i,s,a) for(long long i = s; i<a;++i) #define MP make_pair #define X first #define Y second typedef long long lld; typedef pair<int,int> pii; typedef complex<double> cd; typedef vector<cd> vcd; #define ff first #define dd second #define mp make_pair #define sz size() #define pb push_back using namespace std; struct bigNum{ typedef uint32_t mult_t; typedef uint16_t base_t; static const base_t BASE = 10000; static const int B_DIGS = 4; static const char prFirst[]; static const char prNext[]; int len, cap; base_t* digs; public: bigNum(): len(0), cap(0), digs(NULL) {} //public: explicit bigNum(base_t n) { len = cap = 2; digs = new base_t[cap]; digs[0] = n % BASE; digs[1] = n / BASE; clen(); } void init(int l, base_t* const d, int c = 0) { len = l; cap = c; if (!c) cap = max(len,1); digs = new base_t[cap]; memcpy(digs, d, len * sizeof(base_t)); memset(digs + len, 0, (cap - len) * sizeof(base_t)); clen(); } void extend(int n_cap) { if (cap >= n_cap) return; base_t* n_digs = new base_t[n_cap]; memcpy(n_digs, digs, len * sizeof(base_t)); memset(n_digs + len, 0, (n_cap - len) * sizeof(base_t)); if(digs) delete[] digs; swap(digs, n_digs); cap = n_cap; } void clen() { while (len > 0 && !digs[len - 1]) --len; } bigNum(int l, base_t* const d, int c = 0) { init(l, d, c); } explicit bigNum(const char* s) { int n = strlen(s); cap = len = (n + B_DIGS - 1) / B_DIGS; digs = new base_t[len]; int pos = 0; FORD(i, len-1, 0) { digs[i] = 0; for (; pos < n - (i) * B_DIGS && pos < n; ++pos) { digs[i] = 10 * digs[i] + s[pos] - '0'; } } clen(); } bigNum(bigNum const & b) { init(b.len, b.digs); } ~bigNum() { if (digs) delete[] digs; } bigNum& operator= (const bigNum & b) { delete[] digs; init(b.len, b.digs); return *this; } bool operator == (bigNum const& b) const { if (len != b.len) return false; REP(i, len) { if (digs[i] != b.digs[i]) return false; } return true; } bool operator != (bigNum const& b) const { return ! (operator==(b)); } short compare(bigNum const& b) const { if (len < b.len) return -1; if (len > b.len) return 1; FORD(i, len - 1, 0) { if (digs[i] < b.digs[i]) return -1; if (digs[i] > b.digs[i]) return 1; } return 0; } bool operator < (bigNum const &b) const { return compare(b) < 0; } bool operator > (bigNum const &b) const { return compare(b) > 0; } bool operator <= (bigNum const &b) const { return compare(b) <= 0; } bool operator >= (bigNum const &b) const { return compare(b) >= 0; } bigNum const& operator += (base_t n) { return operator+=(bigNum(n)); } bigNum const & operator+= (bigNum const& b) { extend(max(len, b.len)+1); len = max(len, b.len)+1; digs[0] += b.digs[0]; int i; for (i = 1; i < len; ++i) { if (digs[i-1] >= BASE) { digs[i-1] -= BASE; ++digs[i]; } else if (i >= b.len) break; if (i<b.len) digs[i] += b.digs[i]; } clen(); return *this; } bigNum const operator+(bigNum const& b) const { bigNum res(len, digs, max(len, b.len) + 1); res += b; return res; } bigNum const& operator -= (bigNum const& b) { base_t rem = 0; REP(i,len) { if (i < b.len) rem += b.digs[i]; if (rem > digs[i]) { digs[i] -= (rem-BASE); rem = 1; } else { digs[i] -= rem; rem = 0; } if (rem == 0 && i >= b.len - 1) break; } assert(rem == 0); clen(); return *this; } bigNum const& operator-= (base_t n) { return operator-=(bigNum(n)); } bigNum const operator- (bigNum const& b) const { bigNum res(len, digs); res -= b; return res; } bigNum const& operator *= (base_t n) { if (n >= BASE) return operator *= (bigNum(n)); extend(len+1); ++len; base_t p = 0; REP(i, len) { mult_t m = (mult_t) digs[i]*n + p; digs[i] = m % BASE; p = m / BASE; } clen(); return *this; } bigNum const operator << (int sh) const { bigNum res; res.extend(len + sh); res.len = len + sh; memcpy(res.digs + sh, digs, len * sizeof(base_t)); return res; } bigNum const multSh(base_t n, int sh) const { assert(n < BASE); bigNum res; res.extend(len + sh + 1); res.len = len + 1 + sh; base_t p = 0; FOR(i, sh, sh + len - 1) { mult_t m = (mult_t) digs[i-sh]*n + p; res.digs[i] = m % BASE; p = m / BASE; } res.digs[len+sh] = p; res.clen(); return res; } bigNum const operator * (bigNum const & b) const { //if(len < 1000 && b.len < 10){ bigNum res; res.extend(len + b.len); REP(i, len) { base_t p = 0; REP(j, b.len) { mult_t m = (mult_t)digs[i] * b.digs[j] + p + res.digs[i+j]; res.digs[i+j] = m%BASE; p = m/BASE; } int s = i + b.len; while (p>0) { res.digs[s] += p; if (res.digs[s] >= BASE) { res.digs[s] -= BASE; p = 1; } else p = 0; } } res.len = len + b.len; res.clen(); return res;//} /*else{ bigNum res; vcd A(len); vcd B(b.len); For(i,0,len) A[i] = cd(digs[i],0); For(i,0,B.sz) B[i] = cd(b.digs[i],0); A = conj(A,B); res.len = A.sz; For(i,0,A.sz)res.digs[i] = round(A[i].real()); return res; }*/ ///rip FFT, dokładność się sypie kind of } bigNum const & operator *= (bigNum const & b) { return *this = *this * b; } base_t operator % (base_t m) const { base_t res = 0; FORD(i, len-1, 0) { res = ((mult_t)BASE*res + digs[i]) % m; } return res; } pair<bigNum, bigNum> const div(bigNum const & b) const { bigNum d; int dlen = max(len - b.len + 1, 0); d.extend(dlen); bigNum rem(*this); FORD(i, dlen-1, 0) { base_t l = 0, r = BASE-1; while (l < r) { base_t m = (l + r + 1) / 2; if (rem < b.multSh(m, i)) { r = m - 1; } else { l = m; } } rem -= b.multSh(l,i); if (l > 0) d.digs[i] = l; } d.len = dlen; d.clen(); return MP(d, rem); } bigNum const sqrt() const { bigNum res; int n = (len + 1) / 2; res.extend(n); bigNum rem(*this); FORD(i, n, 0) { base_t l = 0, r = BASE - 1; while ( l <r) { base_t m = (l+r+1)/2; bigNum b(res); b *= 2; b += (bigNum(m) << i); if (rem < b.multSh(m,i)) { r = m - 1; } else l = m; } bigNum ls = bigNum(l).multSh(1,i); bigNum b(res); b *= 2; b += ls; rem -= b.multSh(l,i); res += ls; if (l != 0 && !res.len) res.len = i + 1; } return res; } bigNum const operator/ (bigNum const & b) const { return div(b).X; } bigNum const operator% (bigNum const & b) const { return div(b).Y; } bigNum const & operator /= (bigNum const &b) { return *this = *this / b; } bigNum const & operator %= (bigNum const &b) { return *this = *this % b; } bigNum const operator /= (base_t n) { if (n >= BASE) return *this/bigNum(n); mult_t p = 0; FORD(i, len-1, 0) { p = BASE*p + digs[i]; digs[i] = p / n; p %= n; } clen(); return *this; } friend std::ostream& operator<< (std::ostream& str, bigNum const &n) { if (n.len == 0) str << 0; else { cout << n.digs[n.len-1]; FORD(i, n.len-2, 0) str << setw(4) << setfill('0') << n.digs[i]; } return str; } friend std::istream& operator>> (std::istream& str, bigNum &n) { string s; str >> s; n = bigNum(s.c_str()); return str; } void write() const { if (len == 0) printf("0"); else { printf(prFirst,digs[len-1]); FORD(i, len-2, 0) printf(prNext,digs[i]); } } void writeln() const { write(); putchar('\n'); } }; const char bigNum::prFirst[] = "%u"; const char bigNum::prNext[] = "%.4u"; bigNum const pot(bigNum x, lld ex){ bigNum res("1"); while(ex){ if(ex&1)res *= x,--ex; if(ex) x*=x; ex>>=1; } return res; } vector<int>c[25001]; vector<bigNum>costs[25001]; map<bigNum,int>ile; void dfs(int x, bigNum dist, int p = -1){ //cout<<x<<" "<<p<<" "; //dist.writeln(); if(dist != bigNum("0")) ++ile[dist]; For(i,0,c[x].sz){int s = c[x][i]; if(s!=p){ dfs(s,dist+costs[x][i],x); } } } const lld mod = 1e9+7; void moduj(bigNum x){ lld tmp = 0; for(int i = x.len - 1; i+1; --i){ tmp = tmp * 10000ll + x.digs[i]; tmp %= mod; } printf("%lld",tmp); } int32_t main(void){ char n[20]; int a = 0; int t; //scanf("%d%lld", &a,&t); scanf("%s",n); int ll = strlen(n); For(i,0,ll)a = (a<<3) + (a<<1) + n[i] - '0'; scanf("%d",&t); For(i,0,a-1){ int g,h,w; scanf("%d%d%d", &g,&h,&w); c[g].pb(h); c[h].pb(g); bigNum x = pot(bigNum(a),w); costs[g].pb(x); costs[h].pb(x); } For(i,1,a+1) dfs(i,bigNum("0"));//,puts(""); for(auto s:ile){ //cout<<s.dd/2<<" "; //s.ff.writeln(); if((s).dd/2 >= t){ moduj(s.ff); break; } else t -= (s).dd/2; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | //biblioteczka z bignumami: //https://github.com/mareksom/acmlib/blob/master/code/blazej/bigNum.cpp #include <x86intrin.h> #pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags #pragma GCC target("tune=native") //Enable AVX #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx") #include<stdint.h> #include<bits/stdc++.h> using namespace std; typedef long long lld; #define For(i,s,a) for(lld i = (lld)s; i < (lld)a; ++i) #define FORD(i,s,a) for(long long i = s; i>=a; --i) #define REP(i,a) for(long long i = 0; i<a;++i) #define FOR(i,s,a) for(long long i = s; i<a;++i) #define MP make_pair #define X first #define Y second typedef long long lld; typedef pair<int,int> pii; typedef complex<double> cd; typedef vector<cd> vcd; #define ff first #define dd second #define mp make_pair #define sz size() #define pb push_back using namespace std; struct bigNum{ typedef uint32_t mult_t; typedef uint16_t base_t; static const base_t BASE = 10000; static const int B_DIGS = 4; static const char prFirst[]; static const char prNext[]; int len, cap; base_t* digs; public: bigNum(): len(0), cap(0), digs(NULL) {} //public: explicit bigNum(base_t n) { len = cap = 2; digs = new base_t[cap]; digs[0] = n % BASE; digs[1] = n / BASE; clen(); } void init(int l, base_t* const d, int c = 0) { len = l; cap = c; if (!c) cap = max(len,1); digs = new base_t[cap]; memcpy(digs, d, len * sizeof(base_t)); memset(digs + len, 0, (cap - len) * sizeof(base_t)); clen(); } void extend(int n_cap) { if (cap >= n_cap) return; base_t* n_digs = new base_t[n_cap]; memcpy(n_digs, digs, len * sizeof(base_t)); memset(n_digs + len, 0, (n_cap - len) * sizeof(base_t)); if(digs) delete[] digs; swap(digs, n_digs); cap = n_cap; } void clen() { while (len > 0 && !digs[len - 1]) --len; } bigNum(int l, base_t* const d, int c = 0) { init(l, d, c); } explicit bigNum(const char* s) { int n = strlen(s); cap = len = (n + B_DIGS - 1) / B_DIGS; digs = new base_t[len]; int pos = 0; FORD(i, len-1, 0) { digs[i] = 0; for (; pos < n - (i) * B_DIGS && pos < n; ++pos) { digs[i] = 10 * digs[i] + s[pos] - '0'; } } clen(); } bigNum(bigNum const & b) { init(b.len, b.digs); } ~bigNum() { if (digs) delete[] digs; } bigNum& operator= (const bigNum & b) { delete[] digs; init(b.len, b.digs); return *this; } bool operator == (bigNum const& b) const { if (len != b.len) return false; REP(i, len) { if (digs[i] != b.digs[i]) return false; } return true; } bool operator != (bigNum const& b) const { return ! (operator==(b)); } short compare(bigNum const& b) const { if (len < b.len) return -1; if (len > b.len) return 1; FORD(i, len - 1, 0) { if (digs[i] < b.digs[i]) return -1; if (digs[i] > b.digs[i]) return 1; } return 0; } bool operator < (bigNum const &b) const { return compare(b) < 0; } bool operator > (bigNum const &b) const { return compare(b) > 0; } bool operator <= (bigNum const &b) const { return compare(b) <= 0; } bool operator >= (bigNum const &b) const { return compare(b) >= 0; } bigNum const& operator += (base_t n) { return operator+=(bigNum(n)); } bigNum const & operator+= (bigNum const& b) { extend(max(len, b.len)+1); len = max(len, b.len)+1; digs[0] += b.digs[0]; int i; for (i = 1; i < len; ++i) { if (digs[i-1] >= BASE) { digs[i-1] -= BASE; ++digs[i]; } else if (i >= b.len) break; if (i<b.len) digs[i] += b.digs[i]; } clen(); return *this; } bigNum const operator+(bigNum const& b) const { bigNum res(len, digs, max(len, b.len) + 1); res += b; return res; } bigNum const& operator -= (bigNum const& b) { base_t rem = 0; REP(i,len) { if (i < b.len) rem += b.digs[i]; if (rem > digs[i]) { digs[i] -= (rem-BASE); rem = 1; } else { digs[i] -= rem; rem = 0; } if (rem == 0 && i >= b.len - 1) break; } assert(rem == 0); clen(); return *this; } bigNum const& operator-= (base_t n) { return operator-=(bigNum(n)); } bigNum const operator- (bigNum const& b) const { bigNum res(len, digs); res -= b; return res; } bigNum const& operator *= (base_t n) { if (n >= BASE) return operator *= (bigNum(n)); extend(len+1); ++len; base_t p = 0; REP(i, len) { mult_t m = (mult_t) digs[i]*n + p; digs[i] = m % BASE; p = m / BASE; } clen(); return *this; } bigNum const operator << (int sh) const { bigNum res; res.extend(len + sh); res.len = len + sh; memcpy(res.digs + sh, digs, len * sizeof(base_t)); return res; } bigNum const multSh(base_t n, int sh) const { assert(n < BASE); bigNum res; res.extend(len + sh + 1); res.len = len + 1 + sh; base_t p = 0; FOR(i, sh, sh + len - 1) { mult_t m = (mult_t) digs[i-sh]*n + p; res.digs[i] = m % BASE; p = m / BASE; } res.digs[len+sh] = p; res.clen(); return res; } bigNum const operator * (bigNum const & b) const { //if(len < 1000 && b.len < 10){ bigNum res; res.extend(len + b.len); REP(i, len) { base_t p = 0; REP(j, b.len) { mult_t m = (mult_t)digs[i] * b.digs[j] + p + res.digs[i+j]; res.digs[i+j] = m%BASE; p = m/BASE; } int s = i + b.len; while (p>0) { res.digs[s] += p; if (res.digs[s] >= BASE) { res.digs[s] -= BASE; p = 1; } else p = 0; } } res.len = len + b.len; res.clen(); return res;//} /*else{ bigNum res; vcd A(len); vcd B(b.len); For(i,0,len) A[i] = cd(digs[i],0); For(i,0,B.sz) B[i] = cd(b.digs[i],0); A = conj(A,B); res.len = A.sz; For(i,0,A.sz)res.digs[i] = round(A[i].real()); return res; }*/ ///rip FFT, dokładność się sypie kind of } bigNum const & operator *= (bigNum const & b) { return *this = *this * b; } base_t operator % (base_t m) const { base_t res = 0; FORD(i, len-1, 0) { res = ((mult_t)BASE*res + digs[i]) % m; } return res; } pair<bigNum, bigNum> const div(bigNum const & b) const { bigNum d; int dlen = max(len - b.len + 1, 0); d.extend(dlen); bigNum rem(*this); FORD(i, dlen-1, 0) { base_t l = 0, r = BASE-1; while (l < r) { base_t m = (l + r + 1) / 2; if (rem < b.multSh(m, i)) { r = m - 1; } else { l = m; } } rem -= b.multSh(l,i); if (l > 0) d.digs[i] = l; } d.len = dlen; d.clen(); return MP(d, rem); } bigNum const sqrt() const { bigNum res; int n = (len + 1) / 2; res.extend(n); bigNum rem(*this); FORD(i, n, 0) { base_t l = 0, r = BASE - 1; while ( l <r) { base_t m = (l+r+1)/2; bigNum b(res); b *= 2; b += (bigNum(m) << i); if (rem < b.multSh(m,i)) { r = m - 1; } else l = m; } bigNum ls = bigNum(l).multSh(1,i); bigNum b(res); b *= 2; b += ls; rem -= b.multSh(l,i); res += ls; if (l != 0 && !res.len) res.len = i + 1; } return res; } bigNum const operator/ (bigNum const & b) const { return div(b).X; } bigNum const operator% (bigNum const & b) const { return div(b).Y; } bigNum const & operator /= (bigNum const &b) { return *this = *this / b; } bigNum const & operator %= (bigNum const &b) { return *this = *this % b; } bigNum const operator /= (base_t n) { if (n >= BASE) return *this/bigNum(n); mult_t p = 0; FORD(i, len-1, 0) { p = BASE*p + digs[i]; digs[i] = p / n; p %= n; } clen(); return *this; } friend std::ostream& operator<< (std::ostream& str, bigNum const &n) { if (n.len == 0) str << 0; else { cout << n.digs[n.len-1]; FORD(i, n.len-2, 0) str << setw(4) << setfill('0') << n.digs[i]; } return str; } friend std::istream& operator>> (std::istream& str, bigNum &n) { string s; str >> s; n = bigNum(s.c_str()); return str; } void write() const { if (len == 0) printf("0"); else { printf(prFirst,digs[len-1]); FORD(i, len-2, 0) printf(prNext,digs[i]); } } void writeln() const { write(); putchar('\n'); } }; const char bigNum::prFirst[] = "%u"; const char bigNum::prNext[] = "%.4u"; bigNum const pot(bigNum x, lld ex){ bigNum res("1"); while(ex){ if(ex&1)res *= x,--ex; if(ex) x*=x; ex>>=1; } return res; } vector<int>c[25001]; vector<bigNum>costs[25001]; map<bigNum,int>ile; void dfs(int x, bigNum dist, int p = -1){ //cout<<x<<" "<<p<<" "; //dist.writeln(); if(dist != bigNum("0")) ++ile[dist]; For(i,0,c[x].sz){int s = c[x][i]; if(s!=p){ dfs(s,dist+costs[x][i],x); } } } const lld mod = 1e9+7; void moduj(bigNum x){ lld tmp = 0; for(int i = x.len - 1; i+1; --i){ tmp = tmp * 10000ll + x.digs[i]; tmp %= mod; } printf("%lld",tmp); } int32_t main(void){ char n[20]; int a = 0; int t; //scanf("%d%lld", &a,&t); scanf("%s",n); int ll = strlen(n); For(i,0,ll)a = (a<<3) + (a<<1) + n[i] - '0'; scanf("%d",&t); For(i,0,a-1){ int g,h,w; scanf("%d%d%d", &g,&h,&w); c[g].pb(h); c[h].pb(g); bigNum x = pot(bigNum(a),w); costs[g].pb(x); costs[h].pb(x); } For(i,1,a+1) dfs(i,bigNum("0"));//,puts(""); for(auto s:ile){ //cout<<s.dd/2<<" "; //s.ff.writeln(); if((s).dd/2 >= t){ moduj(s.ff); break; } else t -= (s).dd/2; } } |