• ZOJ1363 Chocolate


    In 2100, ACM chocolate will be one of the favorite foods in the world.
    "Green, orange, brown, red��", colorful sugar-coated shell maybe is the most attractive feature of ACM chocolate. How many colors have you ever seen? Nowadays, it's said that the ACM chooses from a palette of twenty-four colors to paint their delicious candy bits.

    One day, Sandy played a game on a big package of ACM chocolates which contains five colors (green, orange, brown, red and yellow). Each time he took one chocolate from the package and placed it on the table. If there were two chocolates of the same color on the table, he ate both of them. He found a quite interesting thing that in most of the time there were always 2 or 3 chocolates on the table.

    Now, here comes the problem, if there are C colors of ACM chocolates in the package (colors are distributed evenly), after N chocolates are taken from the package, what's the probability that there is exactly M chocolates on the table? Would you please write a program to figure it out?


    Input

    The input file for this problem contains several test cases, one per line.

    For each case, there are three non-negative integers: C (C <= 100), N and M (N, M <= 1000000).

    The input is terminated by a line containing a single zero.


    Output

    The output should be one real number per line, shows the probability for each case, round to three decimal places.


    Sample Input

    5 100 2
    0


    Sample Output

    0.625

    大致意思就是袋子里有C种巧克力,从里面取N块巧克力,如果有两个及以上颜色相同的就将它们全都吃掉,求桌上还剩几种颜色的巧克力

    考虑递推式,就有Pi+1,k=Pi,k-1*(c-k+1)/c+Pi,k+1*(k+1)/c,然而对于这个方程来说,时间复杂度为Θ(nc),跑不过

    那不妨考虑矩阵将递推式转化为矩阵快速幂求解就将复杂度降下来了

    构造矩阵Gn*F,那么其中求得的解就是所求结果

    具体实现如下

     1 #include<bits/stdc++.h>
     2 #define mem0(a) memset(a,0,sizeof(a))
     3 using namespace std;
     4 const int N=110;
     5 struct Mat{
     6     int n;
     7     double m[N][N];
     8     Mat(int n){
     9         this->n=n;mem0(m);
    10     }
    11     Mat operator * (const Mat &b){
    12         Mat res(n);
    13         for(int i=0;i<n;++i)
    14             for(int j=0;j<n;++j)
    15                 for(int k=0;k<n;++k)
    16                     res.m[i][j]+=m[i][k]*b.m[k][j];
    17         return res;
    18     }
    19 };
    20 Mat I(int n){
    21     Mat res(n);
    22     for(int i=0;i<n;++i)res.m[i][i]=1;
    23     return res;
    24 }
    25 Mat FPM(Mat a,int b){
    26     Mat res=I(a.n);
    27     for(;b;b>>=1,a=a*a)if(b&1)res=res*a;
    28     return res;
    29 }
    30 int c,n,m;
    31 int main(){
    32     #ifndef ONLINE_JUDGE
    33         freopen("test.in","r",stdin);freopen("test.out","w",stdout);
    34     #endif
    35     while(~scanf("%d",&c)&&c){
    36         scanf("%d%d",&n,&m);
    37         if(!n&&!m){puts("1.000");continue;}
    38         if(m>n||m>c||(n+m)&1){puts("0.000");continue;}
    39         Mat ff(c+1);
    40         Mat gg(c+1);
    41         ff.m[0][0]=1;
    42         for(int i=0;i<=c;++i){
    43             if(i!=0)gg.m[i][i-1]=(double)(c-i+1)/c;
    44             if(i!=c)gg.m[i][i+1]=(double)(i+1)/c;
    45         }
    46         Mat ans=FPM(gg,n)*ff;
    47         printf("%.3lf
    ",ans.m[m][0]);
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    在Java当中如何优雅地处理临时文件
    lombok插件不建议使用的原因
    linux系统下修改tomcat的端口号时,需要修改的地方
    linux系统下报错为:直接在tomcat的bin目录下生成一个错误文件。
    Linux下修改tomcat端口号
    python实现断点续传下载文件
    Python中下划线---完全解读
    linux  指令 备注
    Linux下高并发socket最大连接数所受的各种限制
    python和pywin32实现窗口查找、遍历和点击
  • 原文地址:https://www.cnblogs.com/ndqzhang1111/p/6751796.html
Copyright © 2020-2023  润新知