zoukankan      html  css  js  c++  java
  • UVa 11971 Polygon (数学,转化)

    题意:一根长度为n的木条,随机选k个位置将其切成k+1段,问这k+1段能组成k+1条边的多边形的概率。

    析:这个题,很明显和 n 是没有任何关系的,因为无论 n 是多少那切多少段都可以,只与切多少段有关。然后我们要转化一下,不能直接做,因为不好做。

    转化为一个圆上选 m+1 个点,能不能组成多边形,很容易知道如果一个边大于一半圆的周长,那就组不成多边形。然后位置是随便选的,概率就是1,

    然后其他 m-1 个点,就只能放那一半上,每个都有1/2的概率,然后 m 个,就是1/(2^m),然后每个点都可能是最长的,所以再乘以(m+1),

    这是组不成的概率,然后用总概率1减去,就是组成的概率。注意long long,刚开始忘了,WA了一次。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    using namespace std ;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f3f;
    const double eps = 1e-8;
    const int maxn = 1e4 + 5;
    const int mod = 1e9 + 7;
    const int dr[] = {0, 0, -1, 1};
    const int dc[] = {-1, 1, 0, 0};
    int n, m;
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    int main(){
        int T;  cin >> T;
        for(int kase = 1; kase <= T; ++kase){
            scanf("%d %d", &n, &m);
            LL a = m + 1;
            LL b = 1LL<<m;
            LL c = b - a;
            LL g = __gcd(c, b);
            printf("Case #%d: %lld/%lld
    ", kase, c / g, b / g);
        }
        return 0;
    }
    
  • 相关阅读:
    算法题---最长公共前缀
    算法练习题---罗马数字转int
    算法练习题---原地删除数组元素
    获取当前服务的IP和端口号
    算法练习题---回文数
    Java数学表示式解析工具- jeval
    Redis的安装与部署
    Centos开机自启动redis
    Java 7 的 7 个新的 “酷” 特性
    java7新特性——使用ThreadLocalRandom产生并发随机数
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5746826.html
Copyright © 2011-2022 走看看