zoukankan      html  css  js  c++  java
  • [牛客OI测试赛2]F假的数学游戏(斯特灵公式)

    题意

    输入一个整数X,求一个整数N,使得N!恰好大于$X^X$。

    Sol

    考试的时候只会$O(n)$求$N!$的前缀和啊。

    不过最后的结论挺好玩的

    $n! approx sqrt{2 pi n} (frac{n}{e})^n$

    然后就可以$O(1)$算啦

    /*
    */
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<set>
    #include<algorithm>
    #include<map>
    #include<cmath>
    #define Pair pair<int, int>
    #define fi first
    #define se second
    #define MP(x, y) make_pair(x, y)
    #define LL long long 
    const LL MAXN = 1e8 + 10, mod = 100000007, inv = 50000004;
    using namespace std;
    inline LL read() {
        char c = getchar(); LL x = 0, f = 1;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x * f;
    }
    const double pi = acos(-1), e = exp(1.0);
    LL N, X;
    double up;
    bool check(double n) {
        return 0.5 * log(2 * pi * n) + n * log(n / e) >= up;
    }
    int main() {
        X = read();
        /*if(X == 7) {printf("10"); return 0;}
        if(X == 77) {printf("94"); return 0;}
        if(X == 777) {printf("892"); return 0;}
        if(X == 7777) {printf("8640"); return 0;}
        if(X == 77777) {printf("84657"); return 0;}
        if(X == 777777) {printf("834966"); return 0;}
        if(X == 7777777) {printf("8267019"); return 0;}
        if(X == 77777777) {printf("82052137"); return 0;}
        if(X == 777777777) {printf("815725636"); return 0;}
        if(X == 7777777777ll) {printf("8092563686"); return 0;}*/
        up = X * log(X);
    //    for(LL i = 1; i <= 1e8; i++) lg[i] = log(i), s[i] = s[i - 1] + lg[i]; 
        //cout << lg[10];
        int times = 51;
        double l = 1, r = 1e13, ans;
        while(times--) {
            LL mid = (l + r) / 2;
            if(check(mid)) ans = mid, r = mid;
            else l = mid;
        }
        cout << (long long)ans;
        return 0;
    }
    /*
    2
    4 6
    4 6
    */
  • 相关阅读:
    bzoj4554: [Tjoi2016&Heoi2016]游戏
    bzoj3166: [Heoi2013]Alo
    luogu3398 仓鼠找sugar
    bzoj3261: 最大异或和
    bzoj3446: [Usaco2014 Feb]Cow Decathlon
    BZOJ1742[Usaco2005 nov]Grazing on the Run 边跑边吃草
    bzoj2750: [HAOI2012]Road
    bzoj4448: [Scoi2015]情报传递
    bzoj2809: [Apio2012]dispatching
    bzoj 1452
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/9602479.html
Copyright © 2011-2022 走看看