Buy the Ticket DP +大数

                          Buy the Ticket

题目抽象:有m个手持50元的人,n个手持100元的人,售票处没有准备钱。问有多少种不同的方案使得购票不中断。每个人是不同的个体。

分析:简单DP题。画个格子,那么选取的点不越过对角线y = x.  dp[i][j]表示i个100元的人,j个手持50元的人的方案数。     i<=j dp[i][j] = dp[i-1][j]+dp[i][j-1];  i>j  dp[i][j] = 0;  如果对某些点有限制,那么在递推是加条件判断。

ans = dp[n][m] * n! * m!;   本题用java处理大数。

 1 import java.util.*;
 2 import java.io.*;
 3 import java.math.*;
 4
 5 public class Main
 6 {
 7     static Scanner cin = new Scanner(new BufferedInputStream(System.in));
 8     public final static int MS= 101;
 9     public  static BigInteger[][] dp = new BigInteger[MS][MS];
10     public  static BigInteger[] fact = new BigInteger[MS];
11     static
12     {
13         fact[0] = BigInteger.ONE;
14         for(int i =1;i<MS;i++)
15             fact[i] = fact[i-1].multiply(BigInteger.valueOf(i));
16
17         for(int i =0;i<MS;i++)
18             dp[0][i] = BigInteger.ONE;
19         for(int i =1;i<MS;i++)
20         {
21             for(int j =0;j<MS;j++)
22             {
23                 if(j<i)
24                     dp[i][j] = BigInteger.ZERO;
25                 else
26                     dp[i][j] = dp[i-1][j].add(dp[i][j-1]);
27             }
28         }
29     }
30
31     //  画出格子,选择的点不能越过y = x。
32     //  dp[i][j] = dp[i-1][j] + dp[i][j-1]   //  dp[i][j]表示i张100元,j张50元的所有情况。
33     // ans = dp[n][m] * n! *m!  ;
34
35     public static void main(String[] args)
36     {
37         int n,m,kase = 1;
38         //System.out.println(dp[0][3].multiply(fact[0]).multiply(fact[3]));
39         while(cin.hasNext())
40         {
41             m = cin.nextInt();
42             n = cin.nextInt();
43
44             if(n == 0 && m == 0)
45                 break;
46             System.out.println("Test #" + (kase++) +":");
47             System.out.println(dp[n][m].multiply(fact[n]).multiply(fact[m]));
48         }
49     }
50 }
时间: 2024-10-18 23:47:32

Buy the Ticket DP +大数的相关文章

Buy the Ticket(卡特兰数+递推高精度)

Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1886 Accepted Submission(s): 832   Problem Description The \\\\\\\"Harry Potter and the Goblet of Fire\\\\\\\" will be on show i

hdu Buy the Ticket

1 import java.math.BigInteger; 2 import java.util.*; 3 public class Main { 4 public static void main(String []args) 5 { 6 Scanner cin=new Scanner(System.in); 7 int n,m,i; 8 int t1=0; 9 while(cin.hasNextBigInteger()) 10 { 11 t1++; 12 m=cin.nextInt();

Buy the Ticket{HDU1133}

Buy the TicketTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6517 Accepted Submission(s): 2720 Problem DescriptionThe "Harry Potter and the Goblet of Fire" will be on show in the next few day

HDU——1133 Buy the Ticket

Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7152    Accepted Submission(s): 2998 Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the nex

[hdu 4933]Miaomiao&#39;s Function 数位DP+大数

Miaomiao's Function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 79    Accepted Submission(s): 18 Problem Description Firstly , Miaomiao define two functions f(x) , g(x): (K is the smallest

Codeforces 938D Buy a Ticket (转化建图 + 最短路)

题目链接  Buy a Ticket 题意   给定一个无向图.对于每个$i$ $\in$ $[1, n]$, 求$min\left\{2d(i,j) + a_{j}\right\}$ 建立超级源点$n+1$, 对于每一条无向边$(x, y, z)$,$x$向$y$连一条长度为$2z$的边,反之亦然. 对于每个$a_{i}$, 从$i$到$n+1$连一条长度为$a_{i}$的边,反之亦然. 然后跑一边最短路即可. #include <bits/stdc++.h> using namespace

Codeforces 938D Buy a Ticket

Buy a Ticket 题意要求:求出每个城市看演出的最小费用, 注意的一点就是车票要来回的. 题解:dijkstra 生成优先队列的时候直接将在本地城市看演出的费用放入队列里, 然后直接跑就好了,  dis数组存的是, 当前情况下的最小花费是多少. 代码: 1 #include<iostream> 2 #include<cstring> 3 #include<string> 4 #include<queue> 5 #include<vector&g

Codeforces 938 D. Buy a Ticket (dijkstra 求多元最短路)

题目链接:Buy a Ticket 题意: 给出n个点m条边,每个点每条边都有各自的权值,对于每个点i,求一个任意j,使得2×d[i][j] + a[j]最小. 题解: 这题其实就是要我们求任意两点的最短路,但是从点的个数上就知道这题不可以用floyd算法,其实多元最短路可以用dijkstra算.@.@!把所有的点的权值和点放到结构体里面,放入优先队列,其实这样就能保证每次拓展到的点就是这个点的最短路(因为是优先队列,保证拓展到的点这时候的值是最小的),其实就是这个点想通就很简单. 1 #inc

D. Buy a Ticket

D. Buy a Ticket Musicians of a popular band "Flayer" have announced that they are going to "make their exit" with a world tour. Of course, they will visit Berland as well. There are n cities in Berland. People can travel between cities