hdu 4873 ZCC Loves Intersection(大数+概率)

题目链接:hdu 4873 ZCC Loves Intersection

题目大意:给出N,D,表示在一个D维的坐标上,坐标范围为0~N-1。在这个坐标系中有D条线段,分别平行与各个坐标轴,每秒会根据题目中的伪代码随机生成各个线段的位置。两条直线相交的话会得一分,问每秒得分的期望。

解题思路:总的情况(ND?1?C(2N))D,两条直线相交的得分C(2D)?s2?ND?2?(ND?2?C(2N))D?2

s是在二维情况下的分的情况数s=∑i=1N((N?i+1) i?1)=N3+3?N2?4?N6

最后化简成ans=C(2D)?(N+4)29?ND

因为分子大于long long 的上限,所以在通分的时候为了避免大数相除,可以将分子差分成质因子来判断。

在高精度成低精度的时候要防止乘的过程中溢出int,因为N很大。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>

using namespace std;
const int MAXN = 10005;

struct bign {
    int len, num[MAXN];

    bign () {
        len = 0;
        memset(num, 0, sizeof(num));
    }
    bign (int number) {*this = number;}
    bign (const char* number) {*this = number;}

    void DelZero ();
    void Put ();

    void operator = (int number);
    void operator = (char* number);

    bool operator <  (const bign& b) const;
    bool operator >  (const bign& b) const { return b < *this; }
    bool operator <= (const bign& b) const { return !(b < *this); }
    bool operator >= (const bign& b) const { return !(*this < b); }
    bool operator != (const bign& b) const { return b < *this || *this < b;}
    bool operator == (const bign& b) const { return !(b != *this); }

    void operator ++ ();
    void operator -- ();
    bign operator + (const int& b);
    bign operator + (const bign& b);
    bign operator - (const int& b);
    bign operator - (const bign& b);
    bign operator * (const int& b);
    bign operator * (const bign& b);
    bign operator / (const int& b);
    //bign operator / (const bign& b);
    int operator % (const int& b);
};

/*Code*/
const int maxn = 5000;
int cnt, num[maxn];

void divfact (int n) {
    int m = (int)sqrt(n + 0.5);

    for (int i = 2; i <= m; i++) {
        if (n % i)
            continue;
        num[cnt++] = i;
        while (n % i == 0)
            n /= i;
    }

    if (n != 1)
        num[cnt++] = n;
}

bign power (bign x, int d) {
    bign ans = 1;
    while (d) {
        if (d & 1)
            ans = ans * x;
        x = x * x;
        d /= 2;
    }
    return ans;
}

int main () {
    int N, D;
    while (scanf("%d%d", &N, &D) == 2 && N + D) {
        cnt = 0;
        bign q = N;
        q = power(q, D) * 9;

        bign p = D * (D - 1) / 2;
        p = p * (N + 4);
        p = p * (N + 4);

        divfact(N);
        num[cnt++] = 3;

        for (int i = 0; i < cnt; i++) {
            while (p % num[i] == 0 && q % num[i] == 0) {
                p = p / num[i];
                q = q / num[i];
            }
        }

        if (p != q) {
            p.Put();
            printf("/");
            q.Put();
        } else
            printf("1");
        printf("\n");
    }
    return 0;
}

void bign::DelZero () {
    while (len && num[len-1] == 0)
        len--;

    if (len == 0) {
        num[len++] = 0;
    }
}

void bign::Put () {
    for (int i = len-1; i >= 0; i--)
        printf("%d", num[i]);
}

void bign::operator = (char* number) {
    len = strlen (number);
    for (int i = 0; i < len; i++)
        num[i] = number[len-i-1] - ‘0‘;

    DelZero ();
}

void bign::operator = (int number) {

    len = 0;
    while (number) {
        num[len++] = number%10;
        number /= 10;
    }

    DelZero ();
}

bool bign::operator < (const bign& b) const {
    if (len != b.len)
        return len < b.len;
    for (int i = len-1; i >= 0; i--)
        if (num[i] != b.num[i])
            return num[i] < b.num[i];
    return false;
}

void bign::operator ++ () {
    int s = 1;

    for (int i = 0; i < len; i++) {
        s = s + num[i];
        num[i] = s % 10;
        s /= 10;
        if (!s) break;
    }

    while (s) {
        num[len++] = s%10;
        s /= 10;
    }
}

void bign::operator -- () {
    if (num[0] == 0 && len == 1) return;

    int s = -1;
    for (int i = 0; i < len; i++) {
        s = s + num[i];
        num[i] = (s + 10) % 10;
        if (s >= 0) break;
    }
    DelZero ();
}

bign bign::operator + (const int& b) {
    bign a = b;
    return *this + a;
}

bign bign::operator + (const bign& b) {
    int bignSum = 0;
    bign ans;

    for (int i = 0; i < len || i < b.len; i++) {
        if (i < len) bignSum += num[i];
        if (i < b.len) bignSum += b.num[i];

        ans.num[ans.len++] = bignSum % 10;
        bignSum /= 10;
    }

    while (bignSum) {
        ans.num[ans.len++] = bignSum % 10;
        bignSum /= 10;
    }

    return ans;
}

bign bign::operator - (const int& b) {
    bign a = b;
    return *this - a;
}

bign bign::operator - (const bign& b) {
    int bignSub = 0;
    bign ans;
    for (int i = 0; i < len || i < b.len; i++) {
        bignSub += num[i];
        bignSub -= b.num[i];
        ans.num[ans.len++] = (bignSub + 10) % 10;
        if (bignSub < 0) bignSub = -1;
    }
    ans.DelZero ();
    return ans;
}

bign bign::operator * (const int& b) {
    long long bignSum = 0;
    bign ans;

    ans.len = len;
    for (int i = 0; i < len; i++) {
        bignSum += (long long)num[i] * b;
        ans.num[i] = bignSum % 10;
        bignSum /= 10;
    }

    while (bignSum) {
        ans.num[ans.len++] = bignSum % 10;
        bignSum /= 10;
    }

    return ans;
}

bign bign::operator * (const bign& b) {
    bign ans;
    ans.len = 0; 

    for (int i = 0; i < len; i++){
        int bignSum = 0;  

        for (int j = 0; j < b.len; j++){
            bignSum += num[i] * b.num[j] + ans.num[i+j];
            ans.num[i+j] = bignSum % 10;
            bignSum /= 10;
        }
        ans.len = i + b.len;  

        while (bignSum){
            ans.num[ans.len++] = bignSum % 10;
            bignSum /= 10;
        }
    }
    return ans;
}

bign bign::operator / (const int& b) {

    bign ans;

    int s = 0;
    for (int i = len-1; i >= 0; i--) {
        s = s * 10 + num[i];
        ans.num[i] = s/b;
        s %= b;
    }

    ans.len = len;
    ans.DelZero ();
    return ans;
}

int bign::operator % (const int& b) {

    bign ans;

    int s = 0;
    for (int i = len-1; i >= 0; i--) {
        s = s * 10 + num[i];
        ans.num[i] = s/b;
        s %= b;
    }

    return s;
}

hdu 4873 ZCC Loves Intersection(大数+概率),布布扣,bubuko.com

时间: 2024-10-01 09:26:06

hdu 4873 ZCC Loves Intersection(大数+概率)的相关文章

HDU 4873 ZCC Loves Intersection(JAVA、大数、推公式)

在一个D维空间,只有整点,点的每个维度的值是0~n-1 .现每秒生成D条线段,第i条线段与第i维度的轴平行.问D条线段的相交期望. 生成线段[a1,a2]的方法(假设该线段为第i条,即与第i维度的轴平行)为,i!=j时,a1[j]=a2[j],且随机取区间[0,n-1]内的整数.然后a1[i],a2[i]在保证a1[i]<a2[i]的前提下同样随机. 由于D条线段各自跟自己维度的轴平行,我们可以转换成只求第i个维度与第j个维度的相交期望,然后乘以C(2,n)就好了 显然线段[a1,a2]和线段[

hdu 5229 ZCC loves strings

题意: CC有N个字符串,他正在和Miss G.用这N个字符串玩一个小游戏.ZCC会从这N个串中等概率随机选两个字符串(不可以是同一个).然后,ZCC和Miss G.轮流操作.Miss G.总是先操作的.在每轮中,操作者可以选择操作A或操作B. 操作A:在两个串中选择一个当前非空的串,然后在这个串的末尾删去一个字符. 操作B: 若当前两个串完全相同且非空,则可以使用这个操作.此时两个串都被清空. 不能操作的玩家输掉了这个游戏. ZCC想要知道他输掉游戏的概率是多少(也就是Miss G.获胜的概率

HDU 4876 ZCC loves cards _(:зゝ∠)_ 随机输出保平安

GG,,,g艹 #include <cstdio> #include <iostream> #include <algorithm> #include <string.h> #include <vector> #include <queue> #include <math.h> using namespace std; vector<int>G[21][7];//G[i][j] 表示n=i k=j的情况下 二进

hdu 4882 ZCC Loves Codefires(数学题+贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4882 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

hdu 4882 ZCC Loves Codefires(贪心)

# include<stdio.h> # include <algorithm> # include <string.h> using namespace std; struct node { int v; int t; }; struct node a[100010]; bool cmp(node a,node b) { return a.v *a.t+(a.v+b.v)*b.t<b.v*b.t+(a.v+b.v)*a.t; } int main() { int

JAVA hdu 4882 ZCC Loves Codefires

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4882 题解:参考题后Discuss javaherongwei 的讲解 考察序列中相邻的两题i, j(i在前).交换它们后,解出它们之前的题目所带来的时间对答案的贡献是不变的,它们对它们后面的题目的贡献也是不变的,其他题目之间对答案的贡献自然也是不变的.唯一的变化就是,原来的EiKj一项变成了EjKi一项.那么,为了使答案变优,需要满足的条件是EjKi≤EiKj.也即Ei/Ki≥Ej/Kj.那么,最

HDU 4876 ZCC loves cards

我决定记录下这么恶心的代码.比赛的时候头晕脑胀,写得好搓,错的地方好多好多,回来调了好久.... 做法大概就是C(20,6)选出卡牌后,再k!枚举排列,再k*k得出该排列能得出什么数字. 当然,光这样做绝对会T,里面加了各种剪枝后就1650ms险过了.. 最主要的剪枝是选出k张牌后,看牌能不能组成L~ans里面各个数字,能才进行下一步.然后k!可以拆成(k-x)!*(x!)..不过这里其实大概没什么大优化吧 #include<cstdio> #include<iostream> #

hdu 4876 ZCC loves cards(暴力)

题目链接:hdu 4876 ZCC loves cards 题目大意:给出n,k,l,表示有n张牌,每张牌有值.选取其中k张排列成圈,然后在该圈上进行游戏,每次选取m(1≤m≤k)张连续的牌,取牌上值的亦或和.要求找到一个圈,使得L~R之间的数都可以得到,输出R.如果R < L输出0. 解题思路:暴力,首先预处理出来每种选取的亦或值,然后在该基础上从可以组成L的状态中挑选一个,L+1的状态中挑取一个,知道说总的挑取出所有状态中选中的牌的个数大于K为值,然后用全排序去查找最大的R. #includ

hdu 4882 ZCC Loves Codefires(贪心)

题目链接:hdu 4882 ZCC Loves Codefires 题目大意:就是CF的比赛,根据时间的推迟会相应的扣掉题目的分数,问说最少扣几分. 解题思路:相邻交换法,判断两个题目之间的比率确定前后位置. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1e5+5; typedef __int64 ll; struct st