lightoj 1079 概率计算+01背包

1079 - Just another Robbery
Time Limit: 4 second(s)Memory Limit: 32 MB

As Harry Potter series is over, Harry has no job. Since hewants to make quick money, (he wants everything quick!) so he decided to robbanks. He wants to make a calculated risk, and grab as much money as possible. Buthis friends - Hermione and Ron have decided upon a tolerable probability Pof getting caught. They feel that he is safe enough if the banks he robstogether give a probability less than P.

Input

Input starts with an integer T (≤ 100),denoting the number of test cases.

Each case contains a real number P, the probability Harryneeds to be below, and an integer N (0 < N ≤ 100), the numberof banks he has plans for. Then follow N lines, where line jgives an integer Mj (0 < Mj ≤ 100) and areal number Pj . Bank j contains Mjmillions, and the probability of getting caught from robbing it is Pj.A bank goes bankrupt if it is robbed, and you may assume that all probabilitiesare independent as the police have very low funds.

Output

For each case, print the case number and the maximum numberof millions he can expect to get while the probability of getting caught isless than P.

Sample Input

Output for Sample Input

3

0.04 3

1 0.02

2 0.03

3 0.05

0.06 3

2 0.03

2 0.03

3 0.05

0.10 3

1 0.03

2 0.02

3 0.05

Case 1: 2

Case 2: 4

Case 3: 6

Note

For the first case, if he wants to rob bank 1 and 2, thenthe probability of getting caught is 0.02 + (1 - 0.02) * .03 = 0.0494which is greater than the given probability (0.04). That's why he hasonly option, just to rob rank 2.


题意: Potter要去抢银行, 每个银行有能抢的金钱数量和被警察抓到的概率, 求在总概率不超过P的情况下能抢到的最大的金钱数.每个银行只能抢一次


分析: dp[i]表示抢i的钱被抓的概率最小是多少, 然后逆着来,保证每个银行值抢一次, 更新最小值.

注意这里被抓的概率不是简单的相加, 而是先计算各自出不被抓的概率, 然后相乘, 最后1.0在减去不被抓的概率就是被抓的概率了,

这点应该很容易理解.

#include<bitset>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))
#define F first
#define S second
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

inline int in()
{
    int res=0;char c;int f=1;
    while((c=getchar())<'0' || c>'9')if(c=='-')f=-1;
    while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
    return res*f;
}
const int N=10010,MOD=1e9+7;

double dp[N],p[123];
int val[132];
int main()
{
    int T=in(),ca=1;
    while(T--)
    {
        double P;
        scanf("%lf",&P);
        int n=in(),sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%lf",&val[i],&p[i]);
            sum += val[i];
        }
        for(int i=0;i<=sum;i++) dp[i]=inf;
        dp[0]=0;
        for(int i=0;i<n;i++)
        {
            double no=1.0-p[i];
            for(int j=sum;j>=val[i];j--)
            {
                if(dp[j-val[i]]!=inf)
                {
                    double tmp = no * (1.0-dp[j-val[i]]);
                    dp[j] = min(1.0-tmp,dp[j]);
                }
            }
        }
        int ans=0;
        for(int i=sum;i;i--)
        {
            if(dp[i]<=P)
            {
                ans=i;
                break;
            }
        }
        printf("Case %d: %d\n",ca++,ans);
    }
    return 0;
}



Sigma函数是指一个数字的所有因子之和。给定一个数字n,需要求出有多少个数字的Sigma函数是偶数。\[2\] 为了解决这个问题,可以先筛选出n范围内的素数(范围在10^6即可),然后对n进行素因子分解。对于每个因子,如果它的Sigma函数中连乘的每一项都是偶数,那么整个Sigma函数就是偶数。具体实现中,可以判断每个因子的平方根是否为偶数,如果是偶数,则减去(平方根+1)/2。\[1\] 另外,还可以使用O(1)的做法来解决这个问题。根据观察,所有的完全平方数及其两倍的值都会导致Sigma函数为偶数。因此,可以直接计算n的平方根,然后减去(平方根+1)/2即可得到结果。\[3\] #### 引用[.reference_title] - *1* [Sigma Function](https://blog.csdn.net/PNAN222/article/details/50938232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【LightOJ1336】Sigma Function(数论)](https://blog.csdn.net/qq_30974369/article/details/79009498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值