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 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
| #include<fstream> #include<string> #include<sstream> using namespace std;
__float128 lgammaq (__float128); __float128 logq (__float128); __float128 powq (__float128, __float128); __float128 scalbnq (__float128, int);
#define M_LN10q 2.3025850929940456840179914546843642Q
typedef union { __float128 value;
struct { unsigned long long mant_low:64; unsigned long long mant_high:48; unsigned exponent:15; unsigned negative:1; } ieee;
struct { unsigned long long low; unsigned long long high; } words64;
struct { unsigned w3; unsigned w2; unsigned w1; unsigned w0; } words32;
struct { unsigned long long mant_low:64; unsigned long long mant_high:47; unsigned quiet_nan:1; unsigned exponent:15; unsigned negative:1; } nan;
} ieee854_float128;
#define GET_FLT128_WORDS64(ix0,ix1,d) \ do { \ ieee854_float128 u; \ u.value = (d); \ (ix0) = u.words64.high; \ (ix1) = u.words64.low; \ } while (0)
#define GET_FLT128_MSW64(v,d) \ do { \ ieee854_float128 u; \ u.value = (d); \ (v) = u.words64.high; \ } while (0)
#define SET_FLT128_MSW64(d,v) \ do { \ ieee854_float128 u; \ u.value = (d); \ u.words64.high = (v); \ (d) = u.value; \ } while (0)
static const __float128 one = 1.0Q; static const __float128 huge = 1.0e4000Q;
static const __float128 ls2pi = 9.1893853320467274178032973640561763986140E-1Q; #define NRASY 12 static const __float128 RASY[NRASY + 1] = { 8.333333333333333333333333333310437112111E-2Q, -2.777777777777777777777774789556228296902E-3Q, 7.936507936507936507795933938448586499183E-4Q, -5.952380952380952041799269756378148574045E-4Q, 8.417508417507928904209891117498524452523E-4Q, -1.917526917481263997778542329739806086290E-3Q, 6.410256381217852504446848671499409919280E-3Q, -2.955064066900961649768101034477363301626E-2Q, 1.796402955865634243663453415388336954675E-1Q, -1.391522089007758553455753477688592767741E0Q, 1.326130089598399157988112385013829305510E1Q, -1.420412699593782497803472576479997819149E2Q, 1.218058922427762808938869872528846787020E3Q };
static __float128 neval (__float128 x, const __float128 *p, int n) { __float128 y;
p += n; y = *p--; do { y = y * x + *p--; } while (--n > 0); return y; }
__float128 lgammaq (__float128 x) { __float128 p, q;
q = ls2pi - x; q = (x - 0.5Q) * logq (x) + q; if (x > 1.0e18Q) return (q);
p = 1.0Q / (x * x); q += neval (p, RASY, NRASY) / x; return (q); }
static const __float128 l3 = 3.333333333333333333333333333333336096926E-1Q, l4 = -2.499999999999999999999999999486853077002E-1Q, l5 = 1.999999999999999999999999998515277861905E-1Q, l6 = -1.666666666666666666666798448356171665678E-1Q, l7 = 1.428571428571428571428808945895490721564E-1Q, l8 = -1.249999999999999987884655626377588149000E-1Q, l9 = 1.111111111111111093947834982832456459186E-1Q, l10 = -1.000000000000532974938900317952530453248E-1Q, l11 = 9.090909090915566247008015301349979892689E-2Q, l12 = -8.333333211818065121250921925397567745734E-2Q, l13 = 7.692307559897661630807048686258659316091E-2Q, l14 = -7.144242754190814657241902218399056829264E-2Q, l15 = 6.668057591071739754844678883223432347481E-2Q;
static const __float128 logtbl[92] = { -5.5345593589352099112142921677820359632418E-2Q, -5.2108257402767124761784665198737642086148E-2Q, -4.8991686870576856279407775480686721935120E-2Q, -4.5993270766361228596215288742353061431071E-2Q, -4.3110481649613269682442058976885699556950E-2Q, -4.0340872319076331310838085093194799765520E-2Q, -3.7682072451780927439219005993827431503510E-2Q, -3.5131785416234343803903228503274262719586E-2Q, -3.2687785249045246292687241862699949178831E-2Q, -3.0347913785027239068190798397055267411813E-2Q, -2.8110077931525797884641940838507561326298E-2Q, -2.5972247078357715036426583294246819637618E-2Q, -2.3932450635346084858612873953407168217307E-2Q, -2.1988775689981395152022535153795155900240E-2Q, -2.0139364778244501615441044267387667496733E-2Q, -1.8382413762093794819267536615342902718324E-2Q, -1.6716169807550022358923589720001638093023E-2Q, -1.5138929457710992616226033183958974965355E-2Q, -1.3649036795397472900424896523305726435029E-2Q, -1.2244881690473465543308397998034325468152E-2Q, -1.0924898127200937840689817557742469105693E-2Q, -9.6875626072830301572839422532631079809328E-3Q, -8.5313926245226231463436209313499745894157E-3Q, -7.4549452072765973384933565912143044991706E-3Q, -6.4568155251217050991200599386801665681310E-3Q, -5.5356355563671005131126851708522185605193E-3Q, -4.6900728132525199028885749289712348829878E-3Q, -3.9188291218610470766469347968659624282519E-3Q, -3.2206394539524058873423550293617843896540E-3Q, -2.5942708080877805657374888909297113032132E-3Q, -2.0385211375711716729239156839929281289086E-3Q, -1.5522183228760777967376942769773768850872E-3Q, -1.1342191863606077520036253234446621373191E-3Q, -7.8340854719967065861624024730268350459991E-4Q, -4.9869831458030115699628274852562992756174E-4Q, -2.7902661731604211834685052867305795169688E-4Q, -1.2335696813916860754951146082826952093496E-4Q, -3.0677461025892873184042490943581654591817E-5Q, 0.0000000000000000000000000000000000000000E0Q, -3.0359557945051052537099938863236321874198E-5Q, -1.2081346403474584914595395755316412213151E-4Q, -2.7044071846562177120083903771008342059094E-4Q, -4.7834133324631162897179240322783590830326E-4Q, -7.4363569786340080624467487620270965403695E-4Q, -1.0654639687057968333207323853366578860679E-3Q, -1.4429854811877171341298062134712230604279E-3Q, -1.8753781835651574193938679595797367137975E-3Q, -2.3618380914922506054347222273705859653658E-3Q, -2.9015787624124743013946600163375853631299E-3Q, -3.4938307889254087318399313316921940859043E-3Q, -4.1378413103128673800485306215154712148146E-3Q, -4.8328735414488877044289435125365629849599E-3Q, -5.5782063183564351739381962360253116934243E-3Q, -6.3731336597098858051938306767880719015261E-3Q, -7.2169643436165454612058905294782949315193E-3Q, -8.1090214990427641365934846191367315083867E-3Q, -9.0486422112807274112838713105168375482480E-3Q, -1.0035177140880864314674126398350812606841E-2Q, -1.1067990155502102718064936259435676477423E-2Q, -1.2146457974158024928196575103115488672416E-2Q, -1.3269969823361415906628825374158424754308E-2Q, -1.4437927104692837124388550722759686270765E-2Q, -1.5649743073340777659901053944852735064621E-2Q, -1.6904842527181702880599758489058031645317E-2Q, -1.8202661505988007336096407340750378994209E-2Q, -1.9542647000370545390701192438691126552961E-2Q, -2.0924256670080119637427928803038530924742E-2Q, -2.2346958571309108496179613803760727786257E-2Q, -2.3810230892650362330447187267648486279460E-2Q, -2.5313561699385640380910474255652501521033E-2Q, -2.6856448685790244233704909690165496625399E-2Q, -2.8438398935154170008519274953860128449036E-2Q, -3.0058928687233090922411781058956589863039E-2Q, -3.1717563112854831855692484086486099896614E-2Q, -3.3413836095418743219397234253475252001090E-2Q, -3.5147290019036555862676702093393332533702E-2Q, -3.6917475563073933027920505457688955423688E-2Q, -3.8723951502862058660874073462456610731178E-2Q, -4.0566284516358241168330505467000838017425E-2Q, -4.2444048996543693813649967076598766917965E-2Q, -4.4356826869355401653098777649745233339196E-2Q, -4.6304207416957323121106944474331029996141E-2Q, -4.8285787106164123613318093945035804818364E-2Q, -5.0301169421838218987124461766244507342648E-2Q, -5.2349964705088137924875459464622098310997E-2Q, -5.4431789996103111613753440311680967840214E-2Q, -5.6546268881465384189752786409400404404794E-2Q, -5.8693031345788023909329239565012647817664E-2Q, -6.0871713627532018185577188079210189048340E-2Q, -6.3081958078862169742820420185833800925568E-2Q, -6.5323413029406789694910800219643791556918E-2Q, -6.7595732653791419081537811574227049288168E-2Q };
static const __float128 ln2a = 6.93145751953125e-1Q, ln2b = 1.4286068203094172321214581765680755001344E-6Q;
__float128 logq (__float128 x) { __float128 z, y, w; ieee854_float128 u, t; unsigned int m; int k, e;
u.value = x; m = u.words32.w0;
e = (int) (m >> 16) - (int) 0x3ffe; m &= 0xffff; u.words32.w0 = m | 0x3ffe0000; m |= 0x10000; if (m < 0x16800) { k = (m - 0xff00) >> 9;
t.words32.w0 = 0x3fff0000 + (k << 9); t.words32.w1 = 0; t.words32.w2 = 0; t.words32.w3 = 0; u.words32.w0 += 0x10000; e -= 1; k += 64; } else { k = (m - 0xfe00) >> 10; t.words32.w0 = 0x3ffe0000 + (k << 10); t.words32.w1 = 0; t.words32.w2 = 0; t.words32.w3 = 0; } if ((x <= 1.0078125Q) && (x >= 0.9921875Q)) { z = x - 1.0Q; k = 64; t.value = 1.0Q; e = 0; } else {
z = (u.value - t.value) / t.value; } w = z * z; y = ((((((((((((l15 * z + l14) * z + l13) * z + l12) * z + l11) * z + l10) * z + l9) * z + l8) * z + l7) * z + l6) * z + l5) * z + l4) * z + l3) * z * w; y -= 0.5 * w; y += e * ln2b; y += z; y += logtbl[k-26]; y += (t.value - 1.0Q); y += e * ln2a; return y; }
static const __float128 bp[] = { 1.0Q, 1.5Q, };
static const __float128 dp_h[] = { 0.0, 5.8496250072115607565592654282227158546448E-1Q };
static const __float128 dp_l[] = { 0.0, 1.0579781240112554492329533686862998106046E-16Q };
static const __float128 two = 2.0Q, two113 = 1.0384593717069655257060992658440192E34Q, tiny = 1.0e-3000Q;
static const __float128 LN[] = { -3.0779177200290054398792536829702930623200E1Q, 6.5135778082209159921251824580292116201640E1Q, -4.6312921812152436921591152809994014413540E1Q, 1.2510208195629420304615674658258363295208E1Q, -9.9266909031921425609179910128531667336670E-1Q }; static const __float128 LD[] = { -5.129862866715009066465422805058933131960E1Q, 1.452015077564081884387441590064272782044E2Q, -1.524043275549860505277434040464085593165E2Q, 7.236063513651544224319663428634139768808E1Q, -1.494198912340228235853027849917095580053E1Q };
static const __float128 PN[] = { 5.081801691915377692446852383385968225675E8Q, 9.360895299872484512023336636427675327355E6Q, 4.213701282274196030811629773097579432957E4Q, 5.201006511142748908655720086041570288182E1Q, 9.088368420359444263703202925095675982530E-3Q, }; static const __float128 PD[] = { 3.049081015149226615468111430031590411682E9Q, 1.069833887183886839966085436512368982758E8Q, 8.259257717868875207333991924545445705394E5Q, 1.872583833284143212651746812884298360922E3Q, };
static const __float128 lg2 = 6.9314718055994530941723212145817656807550E-1Q, lg2_h = 6.9314718055994528622676398299518041312695E-1Q, lg2_l = 2.3190468138462996154948554638754786504121E-17Q, ovt = 8.0085662595372944372e-0017Q, cp = 9.6179669392597560490661645400126142495110E-1Q, cp_h = 9.6179669392597555432899980587535537779331E-1Q, cp_l = 5.0577616648125906047157785230014751039424E-17Q;
__float128 powq (__float128 x, __float128 y) { __float128 z, ax, z_h, z_l, p_h, p_l; __float128 y1, t1, t2, r, s, t, u, v, w; __float128 s2, s_h, s_l, t_h, t_l; int i, j, k, n; unsigned ix; int hx; ieee854_float128 o, p;
p.value = x; hx = p.words32.w0; ix = hx & 0x7fffffff;
ax = x;
n = 0; if (ix < 0x00010000) { ax *= two113; n -= 113; o.value = ax; ix = o.words32.w0; } n += ((ix) >> 16) - 0x3fff; j = ix & 0x0000ffff; ix = j | 0x3fff0000; if (j <= 0x3988) k = 0; else if (j < 0xbb67) k = 1; else { k = 0; n += 1; ix -= 0x00010000; }
o.value = ax; o.words32.w0 = ix; ax = o.value;
u = ax - bp[k]; v = one / (ax + bp[k]); s = u * v; s_h = s;
o.value = s_h; o.words32.w3 = 0; o.words32.w2 &= 0xf8000000; s_h = o.value; t_h = ax + bp[k]; o.value = t_h; o.words32.w3 = 0; o.words32.w2 &= 0xf8000000; t_h = o.value; t_l = ax - (t_h - bp[k]); s_l = v * ((u - s_h * t_h) - s_h * t_l); s2 = s * s; u = LN[0] + s2 * (LN[1] + s2 * (LN[2] + s2 * (LN[3] + s2 * LN[4]))); v = LD[0] + s2 * (LD[1] + s2 * (LD[2] + s2 * (LD[3] + s2 * (LD[4] + s2)))); r = s2 * s2 * u / v; r += s_l * (s_h + s); s2 = s_h * s_h; t_h = 3.0 + s2 + r; o.value = t_h; o.words32.w3 = 0; o.words32.w2 &= 0xf8000000; t_h = o.value; t_l = r - ((t_h - 3.0) - s2); u = s_h * t_h; v = s_l * t_h + t_l * s; p_h = u + v; o.value = p_h; o.words32.w3 = 0; o.words32.w2 &= 0xf8000000; p_h = o.value; p_l = v - (p_h - u); z_h = cp_h * p_h; z_l = cp_l * p_h + p_l * cp + dp_l[k]; t = (__float128) n; t1 = (((z_h + z_l) + dp_h[k]) + t); o.value = t1; o.words32.w3 = 0; o.words32.w2 &= 0xf8000000; t1 = o.value; t2 = z_l - (((t1 - t) - dp_h[k]) - z_h); s = one; y1 = y; o.value = y1; o.words32.w3 = 0; o.words32.w2 &= 0xf8000000; y1 = o.value; p_l = (y - y1) * t1 + y * t2; p_h = y1 * t1; z = p_l + p_h; o.value = z; j = o.words32.w0; i = j & 0x7fffffff; k = (i >> 16) - 0x3fff; n = 0; if (i > 0x3ffe0000) { n = z + 0.5Q; t = n; p_h -= t; } t = p_l + p_h; o.value = t; o.words32.w3 = 0; o.words32.w2 &= 0xf8000000; t = o.value; u = t * lg2_h; v = (p_l - (t - p_h)) * lg2 + t * lg2_l; z = u + v; w = v - (z - u); t = z * z; u = PN[0] + t * (PN[1] + t * (PN[2] + t * (PN[3] + t * PN[4]))); v = PD[0] + t * (PD[1] + t * (PD[2] + t * (PD[3] + t))); t1 = z - t * u / v; r = (z * t1) / (t1 - two) - (w + z * w); z = one - (r - z); o.value = z; j = o.words32.w0; j += (n << 16); if ((j >> 16) <= 0) z = scalbnq (z, n); else { o.words32.w0 = j; z = o.value; } return s * z; }
static const __float128 two114 = 2.0769187434139310514121985316880384E+34Q, twom114 = 4.8148248609680896326399448564623183E-35Q;
__float128 scalbnq (__float128 x, int n) { long long k,hx,lx; GET_FLT128_WORDS64(hx,lx,x); k = (hx>>48)&0x7fff; if (k==0) { if ((lx|(hx&0x7fffffffffffffffULL))==0) return x; x *= two114; GET_FLT128_MSW64(hx,x); k = ((hx>>48)&0x7fff) - 114; } if (k==0x7fff) return x+x;
k = k+n; if (k > 0) {SET_FLT128_MSW64(x,(hx&0x8000ffffffffffffULL)|(k<<48)); return x;} if (k <= -114) k += 114; SET_FLT128_MSW64(x,(hx&0x8000ffffffffffffULL)|(k<<48)); return x*twom114; }
const int tbl_cnt=3; const double tbl[tbl_cnt]={1.0,1.0,2.0};
ifstream fin("fact.in"); ofstream fout("fact.out");
int main() { long long n; int k; fin>>n>>k; if(n<tbl_cnt) { fout.precision(k); fout<<tbl[n]<<"e+0\n"; return 0; } __float128 ans=lgammaq(n+1.0Q)/M_LN10q; unsigned long long len=ans; ans=powq(10.0Q,ans-len+k-1); long long frac=ans; ans-=frac; if(ans>=0.5Q) frac++; stringstream ss; ss<<frac; string s=ss.str(); while(s[s.length()-1]=='0') s.erase(s.length()-1); if(s.length()>1) s.insert(1,1,'.'); fout<<s<<"e+"<<len<<endl; return 0; }
|