POJ 1322 递推+奇偶剪枝

Chocolate

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8800   Accepted: 2306   Special Judge

Description

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

Source

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 using namespace std;
 7
 8 int c,n,m;
 9 double dp[2][10003];
10
11 int main()
12 {
13    // freopen("in.txt","r",stdin);
14     while(~scanf("%d",&c)){
15         if(c==0) break;
16         scanf("%d%d",&n,&m);
17         memset(dp,0,sizeof(dp));
18         if(m>n||m>c||(m+n)%2){//奇偶剪枝
19             printf("0.000\n");
20             continue;
21         }
22         if(n>1000)
23         {
24             n=1000+n%2;
25         }//1000以上都转化为1000或1001计算,因为只保留三位小数
26         dp[0][0]=1.0;dp[1][1]=1.0;
27         for(int i=2;i<=n;i++)
28         {
29              dp[i&1][0]=(dp[(i-1)&1][1])*1.0/c;
30              dp[i&1][c]=(dp[(i-1)&1][c-1])*1.0/c;
31             for(int j=1;j<=i&&j<=c;j++)
32             {
33                dp[i&1][j]=dp[(i-1)&1][j-1]*(c-j+1)*1.0/c+dp[(i-1)&1][j+1]*(j+1.0)/c;
34              }
35         }
36         printf("%.3lf\n",dp[n&1][m]);
37     }
38     return 0;
39 }
时间: 2024-10-13 09:25:52

POJ 1322 递推+奇偶剪枝的相关文章

POJ 2506-Tiling(递推+大数)

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7897   Accepted: 3841 Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tiling of a 2x17 rectangle. Input Input is a sequence of lines,

POJ 2229 递推

Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 7: 1) 1+1+1+1+1+1+1 2) 1+1+1+1+1+2 3) 1+1+1

POJ 1664 放苹果 (递推)

题目链接:http://poj.org/problem?id=1664 dp[i][j]表示i个盘放j个苹果的方案数,dp[i][j] 可以由 dp[i - 1][j] 和 dp[i][j - i] 递推而来. 当盘子的个数大于等于苹果的个数: dp[i - 1][j] :i - 1个盘子放j个苹果,说明i个盘子里最少有一个盘子是空的 dp[i][j - i] :i个盘子都放了苹果,说明有j - i个苹果是随便放置的 否则: dp[i][j] = dp[i - 1][j] 然后没有苹果的盘子的方

POJ 3517 And Then There Was One(约瑟夫环-递推or模拟)

POJ 3517 题目: n  k m 数字1到n成环,先叉数字m,往下数k个,直到最后只有一个数字,输出它. 链表模拟: #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<cmath> #include<vector> #incl

POJ 2506 Tiling(递推+大整数加法)

http://poj.org/problem?id=2506 题意: 思路:递推.a[i]=a[i-1]+2*a[i-2]. 计算的时候是大整数加法.错了好久,忘记考虑1了...晕倒. 1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 using namespace std; 6 7 int n; 8 char s[255][255]; 9 10

poj 2506 Tiling 递推

题目链接: http://poj.org/problem?id=2506 题目描述: 有2*1和2*2两种瓷片,问铺成2*n的图形有多少种方法? 解题思路: 利用递推思想,2*n可以由2*(n-1)的状态加上一块竖放2*1的瓷片转移得来,也可以由2*(n-2)的状态加上一块2*2的瓷片或者加上两块横放的2*1的瓷片转移得来. 可得出递推公式:dp[n] = dp[n-1] + dp[n-2]*2: ac秘诀: (1):从输出样例可以看出要用大数来表示,大概需要90位左右. (2):2*0不是零种

[ACM] POJ 2506 Tiling (递推,大数)

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7487   Accepted: 3661 Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tiling of a 2x17 rectangle. Input Input is a sequence of lines,

POJ 1205 Water Treatment Plants(递推)

题意   建设一条河岸的污水处理系统  河岸有n个城市   每个城市都可以自己处理污水 V   也可以把污水传到相邻的城市处理 >或<   除了你传给我我也传给你这种情况   其它都是合法的   两端的城市不能传到不存在的城市 令d[i]表示有i个城市时的处理方法数  最后一个城市的处理方法有 1.V 自己处理自己的  与前i-1个城市的处理方法无关  有d[i-1]种方法 2.< 给左边的城市去处理  也与前i-1个城市的处理方法无关  把自己的污水给第i-1个城市就行了  有d[i-

poj 2229 【完全背包dp】【递推dp】

poj 2229 Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 21281   Accepted: 8281 Description Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an