美文网首页
HDU4762——Cut the Cake

HDU4762——Cut the Cake

作者: xz闲语岁月 | 来源:发表于2017-08-23 21:09 被阅读0次

    Problem

    MMM got a big big big cake, and invited all her M friends to eat the cake together. Surprisingly one of her friends HZ took some (N) strawberries which MMM likes very much to decorate the cake (of course they also eat strawberries, not just for decoration). HZ is in charge of the decoration, and he thinks that it's not a big deal that he put the strawberries on the cake randomly one by one. After that, MMM would cut the cake into M pieces of sector with equal size and shape (the last one came to the party will have no cake to eat), and choose one piece first. MMM wants to know the probability that she can get all N strawberries, can you help her? As the cake is so big, all strawberries on it could be treat as points.

    Input

    First line is the integer T, which means there are T cases.
    For each case, two integers M, N indicate the number of her friends and the number of strawberry.
    (2 < M, N <= 20, T <= 400)

    Output

    As the probability could be very small, you should output the probability in the form of a fraction in lowest terms. For each case, output the probability in a single line. Please see the sample for more details.

    Sample Input

    2
    3 3
    3 4

    Sample Output

    1/3
    4/27


    思路

    先考虑第一颗草莓,放在某块蛋糕上的概率是1/m,其余n-1块草莓跟他在一起的概率是1/m^(n-1),由于第一颗草莓有n种选择,最终结果化简就是 n/(m(n-1))。此题由于数据最大为2019,所以需要用到高精度,用了自己存的高精度模板。

    代码

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <vector>
    #include <string>
    using namespace std;
    
    static const int BASE = 100000000;
    static const int WIDTH = 8;
    
    class BigInteger {
    private:    
        std::vector<int> s;
    public:
        BigInteger(unsigned long long num = 0) { *this = num; }
        BigInteger(std::string s) { *this = s; }
        BigInteger& clean();
        BigInteger& operator = (long long num);
        BigInteger& operator = (const std::string& str);
        BigInteger operator + (const BigInteger& b) const;
        BigInteger operator - (const BigInteger& b) const;
        BigInteger operator * (const BigInteger& b) const;
        BigInteger operator / (const BigInteger& b) const;
        BigInteger operator % (const BigInteger& b) const;
        BigInteger& operator += (const BigInteger& b) { *this = *this + b; return *this; }
        BigInteger& operator -= (const BigInteger& b) { *this = *this - b; return *this; }
        BigInteger& operator *= (const BigInteger& b) { *this = *this * b; return *this; }
        BigInteger& operator /= (const BigInteger& b) { *this = *this / b; return *this; }
        BigInteger& operator %= (const BigInteger& b) { *this = *this % b; return *this; }
        bool operator <(const BigInteger& b) const;
        bool operator >(const BigInteger& b) const { return b < *this; }
        bool operator<=(const BigInteger& b) const { return !(b < *this); }
        bool operator>=(const BigInteger& b) const { return !(*this < b); }
        bool operator!=(const BigInteger& b) const { return b < *this || *this < b; }
        bool operator==(const BigInteger& b) const { return !(b < *this) && !(b > *this); }
        friend std::ostream& operator << (std::ostream& out, const BigInteger& x);
        friend std::istream& operator >> (std::istream& in, BigInteger& x);  
        int bsearch(const BigInteger& b, const BigInteger& m) const;
    };
    
    BigInteger& BigInteger::clean() { 
        while (!s.back() && s.size()>1)
            s.pop_back(); 
        return *this; 
    }
    
    BigInteger& BigInteger::operator = (long long num) {
        s.clear();
        do {
            s.push_back(num % BASE);
            num /= BASE;
        } while (num > 0);
        return *this;
    }
    
    BigInteger& BigInteger::operator = (const std::string& str) {
        s.clear();
        int x, len = (str.length() - 1) / WIDTH + 1;
        for (int i = 0; i < len; i++) {
            int end = str.length() - i*WIDTH;
            int start = std::max(0, end - WIDTH);
            sscanf(str.substr(start, end - start).c_str(), "%d", &x);
            s.push_back(x);
        }
        return (*this).clean();
    }
    
    BigInteger BigInteger::operator + (const BigInteger& b) const {
        BigInteger c; 
        c.s.clear();
        for (size_t i = 0, g = 0; ; i++) {
            if (g == 0 && i >= s.size() && i >= b.s.size()) break;
            int x = g;
            if (i < s.size()) x += s[i];
            if (i < b.s.size()) x += b.s[i];
            c.s.push_back(x % BASE);
            g = x / BASE;
        }
        return c;
    }
    
    BigInteger BigInteger::operator - (const BigInteger& b) const {
        BigInteger c; 
        c.s.clear();
        for (size_t i = 0, g = 0; ; i++) {
            if (g == 0 && i >= s.size() && i >= b.s.size()) break;
            int x = s[i] + g;
            if (i < b.s.size()) x -= b.s[i];
            if (x < 0) { g = -1; x += BASE; }
            else g = 0;
            c.s.push_back(x);
        }
        return c.clean();
    }
    
    BigInteger BigInteger::operator * (const BigInteger& b) const {
        size_t i, j;
        unsigned long long g;
        std::vector<unsigned long long> v(s.size() + b.s.size(), 0);
        BigInteger c; 
        c.s.clear();
        for (i = 0; i<s.size(); i++) 
            for (j = 0; j<b.s.size(); j++) 
                v[i + j] += (unsigned long long)(s[i])*b.s[j];
        for (i = 0, g = 0; ; i++) {
            if (g == 0 && i >= v.size()) break;
            unsigned long long x = v[i] + g;
            c.s.push_back(x % BASE);
            g = x / BASE;
        }
        return c.clean();
    }
    
    BigInteger BigInteger::operator / (const BigInteger& b) const {
        BigInteger c = *this;      
        BigInteger m;              
        for (int i = s.size() - 1; i >= 0; i--) {
            m = m*BASE + s[i];
            c.s[i] = bsearch(b, m);
            m -= b*c.s[i];
        }
        return c.clean();
    }
    
    BigInteger BigInteger::operator % (const BigInteger& b) const {
        BigInteger c = *this;
        BigInteger m;
        for (int i = s.size() - 1; i >= 0; i--) {
            m = m*BASE + s[i];
            c.s[i] = bsearch(b, m);
            m -= b*c.s[i];
        }
        return m;
    }
    
    bool BigInteger::operator < (const BigInteger& b) const {
        if (s.size() != b.s.size()) return s.size() < b.s.size();
        for (int i = s.size() - 1; i >= 0; i--)
            if (s[i] != b.s[i]) return s[i] < b.s[i];
        return false;
    }
    
    std::ostream& operator << (std::ostream& out, const BigInteger& x) {
        out << x.s.back();
        for (int i = x.s.size() - 2; i >= 0; i--) {
            char buf[20];
            sprintf(buf, "%08d", x.s[i]);
            for (size_t j = 0; j < strlen(buf); j++) out << buf[j];
        }
        return out;
    }
    
    std::istream& operator >> (std::istream& in, BigInteger& x) {
        std::string s;
        if (!(in >> s)) return in;
        x = s;
        return in;
    }
    
    int BigInteger::bsearch(const BigInteger& b, const BigInteger& m) const {
        int L = 0, R = BASE - 1, x;
        while (1) {
            x = (L + R) >> 1;
            if (b*x <= m) { 
                if (b*(x + 1)>m) return x; 
                else L = x; 
            }
            else R = x;
        }
    }
    
    int main(){
      int t;cin>>t;
      size_t M,n;
      while(t--){
        cin>>M>>n;
        BigInteger m=M,res=1;
        for(size_t i=1;i<n;i++)res*=m;
        for(size_t i=n;i>=2;i--){  
          if(n%i==0&&res%i==0){  
            n/=i;res/=i;  
          }  
          if(n==1)break;  
        }
        cout<<n<<'/'<<res<<endl;
      }
      return 0;
    }
    

    相关文章

      网友评论

          本文标题:HDU4762——Cut the Cake

          本文链接:https://www.haomeiwen.com/subject/kqvjdxtx.html