Coins [POJ1742] [DP]】的更多相关文章

  Description 给出硬币面额及每种硬币的个数,求从1到m能凑出面额的个数. Input 多组数据,每组数据前两个数字为n,m.n表示硬币种类数,m为最大面额,之后前n个数为每种硬币的面额,后n个数为相应每种硬币的个数. (n<=100,m<=100000,面额<=100000,每种个数<=1000) Output 如题 Sample Input 3 10 1 2 4 2 1 1 2 5 1 4 2 1 0 0 Sample Output 8 4 Solution 这是典…
一开始没多想,虽然注意到数据N<=10^4的范围,想PAT的应该不会超时吧,就理所当然地用dfs做了,结果最后一组真的超时了.剪枝啥的还是过不了,就意识到肯定不是用dfs做了.直到看到别人说用01背包的思路,果真好久没做题了智力水平下降,且原本dp就是我的弱项,压根就没把这题往dp上去想额... (http://www.liuchuo.net/archives/2323) 题意:从n个硬皮中选取方案,使得总和价值正好为m,如果有多种,方案为排列最小的那个. 可以把硬币看成w=v(即容量=价值)的…
Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description People in Silverland use square coins. Not only they have square shapes but also their values are square numbers. Coins with values o…
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact pri…
题意大概是:给出N个硬币, 面值为a_i, 问要凑成X元哪些硬币是不可或缺的.1 ≤ N ≤ 200, 1 ≤ x ≤ 10^4 直接枚举, 然后就是01背包了. 为了不让复杂度多乘个N, 我们就从左往右, 从右往左分别dp一次.这样判断一个硬币就是O(X).总时间复杂度O(NX) ----------------------------------------------------------------------------- #include<bits/stdc++.h>   usi…
I - Coins Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement Let NN be a positive odd number. There are NN coins, numbered 1,2,…,N1,2,…,N. For each ii (1≤i≤N1≤i≤N), when Coin ii is tossed, it comes up heads with probabi…
[CF944G]Coins Exhibition 题意:Jack去年参加了一个珍稀硬币的展览会.Jack记得一共有 $k$ 枚硬币,这些硬币排成一行,从左到右标号为 $1$ 到 $k$ ,每枚硬币是正面朝上的或是反面朝上的.但是Jack不记得每枚硬币具体是正面朝上还是反面朝上了.但是Jack隐约记得,在某些区间里,至少有一枚正面朝上的:以及在某些区间里,至少有一枚反面朝上的.现在Jack想知道,有多少种可能的硬币序列,满足他所记得的所有条件.两个序列被认为是不同的,当且仅当至少存在一个位置 $i…
Alice and Bob are playing a simple game. They line up a row of nn identical coins, all with the heads facing down onto the table and the tails upward. For exactly mm times they select any kk of the coins and toss them into the air, replacing each of…
Coins 题意:一开始所有n个硬币都是反面朝上的,每次必须拿k个来抛,抛的人足够聪明,问m次之后向上的硬币的期望. 首先说了这个足够聪明的意思,就是只要向反面的有k个就不会sb地去拿向正面的来抛,想了一会之后就觉得是个概率dp的转移, 然而一开始想漏了个组合数的加权,但在+1的提醒下搞通了,但是分析了下,这是nmk的时间复杂度, 1e6还有个1e3的大T,emmm理论上会TLE的,但结果看网上题解,都是跟自己思路差不多,所以也是很迷. 嗯,转移过程其实很简单,dp[i][j]就是第i次抛了之后…
Time limit 3000 ms Memory limit 30000 kB Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a ne…
题目链接:Coins Description Alice and Bob are playing a simple game. They line up a row of nn identical coins, all with the heads facing down onto the table and the tails upward. For exactly mm times they select any kk of the coins and toss them into the…
Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15764    Accepted Submission(s): 10843 Problem Description People in Silverland use square coins. Not only they have square shapes bu…
题目链接:POJ - 1742 题目大意 现有 n 种不同的硬币,每种的面值为 Vi ,数量为 Ni ,问使用这些硬币共能凑出 [1,m] 范围内的多少种面值. 题目分析 使用一种 O(nm) 的 DP (据说这是类多重背包?),枚举每一种硬币,对于每一种硬币 i 枚举每一个面值 j ,如果这个面值 j 使用前 i-1 种硬币已经可以凑出,就直接跳过,否则尝试加入一个硬币 i ,看是否能凑出 j .需要满足 (f[j - Vi] == true) && (UseNum[j - Vi] +…
题意:给你N种硬币,每种硬币有Si个,有Pi 概率朝上,每次抛所有硬币抛起,所有反面的拿掉,问每种硬币成为最后的lucky硬币的概率. 题解:都知道是概率dp,但是模拟赛时思路非常模糊,很纠结,dp[10][1e6]这个状态让我觉得丝毫没有用,当时一直以为每种硬币是互相独立的.然后中间吃了个饭,回来又想了很久很久,就是不想看题解,然后发现,这个dp和极限有关,同时状态跟走了几步有极大的关系.否则你初始值根本没法赋,那么显然我猜既然保留6位,肯定当数字小到一定程度时是可以忽略的,也就是进行的次数多…
挺不错的概率DP,看似基础,实则很考验扎实的功底 这题很明显是个DP,为什么???找规律或者算组合数这种概率,N不可能给的这么友善... 因为DP一般都要在支持N^2操作嘛. 稍微理解一下,这DP[i][j]还是不好想啊,首先是写DP[I][j]的含义 首先我们想这道题是要求一个最优决策下的期望,那么这个我们的最优决策是什么??? 决策就是:我们假设我这一次需要翻转K个硬币,我们不愿翻那些已经在正面的,而去翻那些没有在正面的 而如果剩余的反面的不足,我再去翻转正面的 那么给dp[i][j]一个含…
题目链接 \(Description\) 有n(n<=10)种硬币,已知每种硬币的数量和它抛一次正面朝上的概率pi.进行如下过程:每次抛一次所有硬币,将正面朝下的硬币去掉.重复该过程直到只剩一种硬币或是没有硬币. 如果结束时还剩下一种硬币,那称它是 \(LuckyCoin\).求每种硬币成为 \(LuckyCoin\) 的概率. \(Solution\) 我们只需要枚举在第j轮,硬币i死亡(这个词形象233),其它硬币在第j轮之前死亡的概率. 由给出的概率及六位小数可以看出,枚举到100轮就很够…
题目大意 有n个数字,选出一个子集,有q个询问,求子集和模m等于0的方案数%1000000009.(n <= 100000,m <= 100,q <= 30) 假设数据很小,我们完全可以做一个背包. 我们沿着背包的思路,看能不能给物品分一下类,由于m比较小,完全按N个数字模M后的值进行分类,这样就变成了一个多重背包的问题.(转移时要乘上一个组合数) 这时候的时间复杂度是n*m,还是不能过. 对于DP时所枚举到的模m后余数j,它所进行的状态转移是一定的,如果把这些转移先预处理出来,时间复杂…
题意:有\(n\)枚硬币,每枚硬币抛完后向上的概率为\(p[i]\),现在求抛完后向上的硬币个数大于向下的概率. 题解:我们用二维的\(dp[i][j]\)来表示状态,\(i\)表示当前抛的是第\(i\)个硬币,\(j\)表示的是前\(i\)个硬币中向上的个数,那么状态可以表示为,如果\(j=0\),那么\(dp[i][j]=dp[i-1][j]*(1-p[i])\),否则,\(dp[i][j]=dp[i-1][j-1]*p[i]+dp[i-1][j]*(1-p[i])\).即类似01背包的思路…
题目链接:https://codeforc.es/gym/101606/problem/F 题解: 假设 $f[i][j]$ 表示抛 $i$ 次硬币,有 $j$ 个硬币正面朝上的概率. 所以只有两种挑选硬币的情况: 1.正面硬币数量为 $[0,n-1]$,选择反面硬币抛,则正面硬币数量比原本增加 $1$ 或者不变. 2.正面硬币数量为 $n$,随便选择一个硬币抛,则正面硬币数量比原本减少 $1$ 或者不变. 因此可得状态转移方程: 对于 $j<n$,有 f[i+][j+]+=f[i][j]*][…
  用硬币换钱 题目大意:就是有面值为A1,A2,A3....的硬币,各有C1,C2,C3...的数量,问在钱数为m的范围内,能换多少钱?(不找零) 这题看名字就知道是完全背包,但是这题又有点不一样,因为这题的硬币数不是无限的,所以我们要用点特殊的思路 因为这题要我们求的是可以整换的钱的面值数,我们只要保证位置合法就可以了,所以dp矩阵内我们可以以面值数的剩余量为dp量,对于j<面值的位置,我们全部设置为当前面值的硬币数量,当j>=面值时,我们考虑两个位置:1:当上一个位置的这个位置合法(也就…
A - Frog 1/B - Frog 2 入门... #include<cstdio> #define abs(a) ((a)>=0?(a):(-(a))) #define min(a,b) ((a)<(b)?(a):(b)) #define maxn 100050 using namespace std; int dp[maxn],a[maxn]; int main(){ ; scanf("%d",&n); ;i<=n;i++) scanf(&…
Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: f…
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins,…
Contest Website : atcoder.jp/contests/dp \[\begin{array}{c|C|c|c} TaskNum & TaskName & Status & Algorithm \\ \hline A & Frog 1 & \color{green}{AC} & \text{简单线性DP} \\ \hline B & Frog 2 & \color{green}{AC} & \text{简单线性DP,…
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins,…
动态规划里例题,硬币问题. p[i] = dp[i - coin[j]] + 1; 注意i < coin[j] dp[i-coin[j]]无解都要跳过. public class Solution { public int coinChange(int[] coins, int amount) { int[] dp = new int[amount+1]; Arrays.fill(dp,Integer.MAX_VALUE); //dp[i] = dp[i - coin[j]] + 1; dp[0…
题目 题目:CoinChange 有面额不等的coins,数量无限,要求以最少的\(coins\)凑齐所需要的\(amount\). 若能,返回所需的最少coins的数量,若不能,返回-1. Example 1: coins = [1, 2, 5], amount = 11 return 3 (11 = 5 + 5 + 1) Example 2: coins = [2], amount = 3 return -1. 无法用贪心做,例如:coins = [5,6,10], amount = 11…
You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin. Note: You can assume that…
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins,…
You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin. Example 1: Input: amount =…