11181 - Probability|Given

本文介绍了一种使用贝叶斯公式解决特定概率问题的方法,通过递归穷举策略优化算法,避免重复计算,实现高效求解。
部署运行你感兴趣的模型镜像

根据贝叶斯公式:(百度百科) ,即条件概率的一个推广。   我们需要求出每个人在B这个前提条件下的概率,再除以tot(r个人买东西的概率) 即可得到答案。一开始用递归穷举,结果超时,后来发现,原来我是老老实实的枚举了一遍,但是这个过程中可能存在大量的重复现象。正确的策略是只枚举当前人之后的人。还有一点需要注意的是当n = 1,r = 0 ,P = 1.0 这个数据是不存在的。 因为这时会发生除0错误。所以下面代码中有一些是不必要的。

#include<bits/stdc++.h>
using namespace std;
int n,r,A[25];
double p[25],sum[25],tot;
void dfs(int k ,int cur) {
    if(cur==r) {
        double cur_p=1;
        for(int i=1;i<=n;++i){
            if(!A[i]) cur_p*=(1-p[i]);
            else cur_p*=p[i];
        } tot+=cur_p;
        for(int i=1;i<=n;++i)
            if(A[i]==1) sum[i]+=cur_p;
        
    }
    else for(int i=k;i<=n;++i) {
        if(!A[i]) {
            A[i]=1;
            dfs(i+1,cur+1);
            A[i]=0;
        }
    }
}
int main() {
    int maxn = 0;
    while(scanf("%d%d",&n,&r)!=EOF) {
        if(n==0&&r==0) return 0;
        for(int i=1;i<=n;i++) scanf("%lf",&p[i]);
        memset(A,0,sizeof(A));
        memset(sum,0,sizeof(sum));
        printf("Case %d:\n",++maxn);
        tot = 0;
        dfs(1,0);
        if(tot==0) for(int i=1;i<=n;i++) //这个部分是不需要的。
            printf("%.6lf\n",0.0);
        else for(int i=1;i<=n;i++)
            printf("%.6lf\n",sum[i]/tot);
    }
    return 0;
}


您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

IPv4/6 Multicast (S, G, V) Bridging IGMP and MLD are the protocols used by IPv4 and IPv6 nodes respectively, to report their Multicast group membership to neighboring Multicast routers. IGMP or MLD snooping is a common technique used to conserve bandwidth on switch ports where no node has expressed interest in receiving the specific Multicast traffic. This is in contrast to the normal switch behavior, where Unregistered Multicast traffic is forwarded to all bridge ports in the VLAN group. IGMPv1/2 and MLDv1 support Join requests to receive traffic for a given IPM Group, i.e (*, G). This is known as Any-Source Multicast (ASM). IGMPv3 and MLDv2 support Join requests for receive traffic for an IPM Group from a list of senders, a list of (S, G) pairs. This is known as Source-Specific Multicast (SSM). MAC Multicast FDB entries can be used for limiting the forwarding of IPv4/6 Multicast groups, but this approach suffers from two problems:  MAC group address ambiguity: There is a many-to-one mapping between IPv4/6 group addresses and the corresponding MAC group address.  No source-based lookup: The Bridge lookup is based on MAC DA + VLAN and does not include the IP source. To overcome these limitations IP packets can be triggered for IP Multicast Bridging. In IP Multicast Bridging, the FDB destination lookup can be based on the packet source IP (S) and/or the destination IP group address (G), and the packet’s <FID> (directly mapped from the packet’s eVLAN).. IPv4 packets are subject to IP Multicast Bridging if:  IPv4 Multicast Bridging is enabled for the eVLAN and  MAC DA is in the range 01:00:5e:xx:xx:xx/23, and  IP destination address is multicast Otherwise, the packet is forwarded based on its MAC DA FDB lookup. IPv6 packets are subject to IP Multicast Bridging if:  IPv6 Multicast Bridging is enabled for the eVLAN and  MAC DA is in the range 33:33:xx:xx:xx:xx/16 and  IP destination address is multicast Otherwise, the packet is forwarded based on its MAC DA FDB lookup. The IPv4/6 Multicast Bridging mode is set on a per-eVLAN basis to either one of the following:  (S, G, FID): This mode is used for SSM snooping. The FDB lookup is based on the packet source IP (SIP), group destination IP (DIP), and eVLAN-ID (Mapped to FID for the FDB lookup).  (*, G, FID): This mode is used for ASM snooping. The FDB lookup is based on the packet group destination IP (DIP), and eVLAN-ID (Mapped to FID for the FDB lookup). Based on IGMP/MLD report messages trapped to the CPU (Section 10.8.5, IGMP), the CPU configures a Multicast table entry with the appropriate port membership (10.5 "Bridge eVIDX Multicast Group"), and then configures an FDB (S, G, FID) IPv4 or IPv6 Multicast entry associated with the eVIDX index for this group (Section 10.4.7, CPU Update and Query of the FDB). If the IPv4/6 Multicast mode is (*, G, FID), the FDB <SIP> field must be set to 0. The FDB <SIP> and <DIP> are 32-bit fields. For IPv4 Multicast bridging, the full IPv4 address is used in the FDB match lookup. However, for IPv6, which uses a128-bit address size, four octets must be selected from the IPv6 address, to be used for the FDB match lookup. The IPv6 selected octets used for the <SIP> and <DIP> FDB match lookup IPv6 Multicast bridging are configured globally. For each, IPv6 SIP and DIP, four of the sixteen octets in the IPv6 address are configurable. The IPv6 SIP default octet selection is octets 0, 1, 2, and 5 (where octet 0 is the IP address least significant octet). In cases where the IPv6 SIP is derived from the node’s MAC Address (RFC 2464), these octets include the four least significant octets of the node’s MAC Address. The assumption is that this four-octet selection provides a very high probability of uniqueness. The IPv6 DIP default octet selection is octets 0, 1, 2, and 3. This corresponds to the least significant octets of the IPv6 group address. Note that, according to RFC 3307 - IP Multicast allocation Guidelines, the IPv6 group address allocations are from the least significant 32-bit range of the 128-bit Group ID. Based on this assumption, the selection of the least significant four octets provides an exact match of the group address. In the event that multiple IPv6 Multicast flows collide to the same FDB (S, G, V) entry due to the fact that only 32 of the 128 bits of the S and G are represented, the entry’s associated eVIDX group must be configured with a superset of the port members for all the colliding IPv6 Multicast flows. Configuration  In the Ingress Bridge eVLAN Table (Table 393 p. 2336): • To enable/disable IPv4 Multicast-based bridging, set/clear the <IPv4IpmBridgingEn> bit. • To enable/disable IPv6 Multicast-based bridging, set/clear the <IPv6LPMBridgingEn> bit. • To set the IPv4 Multicast bridging mode to ASM or SSM, configure the <IPv4IpmBridgingMode> bit accordingly. • To set the IPv6 Multicast bridging mode to ASM or SSM, configure the <IPv6IpmBridgingMode> bit accordingly.  To configure the IPv6 DIP and SIP octets to be selected for the FDB IPv6 Multicast lookup, set the fields in the IPv6 MC Bridging Bytes Selection Configuration Register (Table 328 p. 2228) accordingly.  To configure FDB IPv4/6 Multicast entries, set the following in the FDB Entry: FDB Entry (Table 436 p. 2460): • Set the FDB <DIP> to the IPM group address. • Set the FDB <Entry Type> to IPv4 or IPv6 Multicast. • If this is a (*, G, V) entry, set the FDB <SIP> to 0, otherwise set it to the source IP address. If IPv6, set the field according to the 4 selected bytes of the IPv6 SIP. • Add the entry to the FDB according to Section 10.4.7, CPU Update and Query of the FDB.翻译一下并解释,按我原来发的格式,不要按上面的格式
11-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值