HDU - 1059 Dividing (母函数二进制优化)/求哪些数能被组合出来

解决Marsha和Bill如何公平分配不同价值弹珠的问题,通过编程实现判断能否将弹珠分为两个等值集合。

Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value. 
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles. 
InputEach line in the input describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., n6, where ni is the number of marbles of value i. So, the example from above would be described by the input-line ``1 0 1 2 0 0''. The maximum total number of marbles will be 20000. 

The last line of the input file will be ``0 0 0 0 0 0''; do not process this line. 
OutputFor each colletcion, output ``Collection #k:'', where k is the number of the test case, and then either ``Can be divided.'' or ``Can't be divided.''. 

Output a blank line after each test case. 
Sample Input
1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0
Sample Output
Collection #1:
Can't be divided.

Collection #2:
Can be divided.

题意 : 有 价值为 1,2,3,4,5,6,价值 的弹珠w[i]个,问是否能平分为2份 ,让两份价值相同

解:显然用普通的母函数数据太大会超时,我们用v数组来标记是否能组成该数字

我们可以用sum统计为所以弹珠的总价值,如果总价值为奇,显示不能平分

否则我们sum=sum/2,我们显然要看最后v[sum]=1,那么能平分

什么是二进制优化: 比如我们有100个价值为3的弹珠 

那么我们可以把它分为   1+2+4+8+16+32+37   因为这7个数能组成100以内任何数,你可以表示你要取几个价值为3的弹珠

而不用一个一个加

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<math.h>
#include<queue>
#include<map>
#include<vector>
#include<set>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
int v[3511330];
int w[110];
int main()
{
    int yy=1;
 while(~scanf("%d%d%d%d%d%d",&w[1],&w[2],&w[3],&w[4],&w[5],&w[6])&&w[1]+w[2]+w[3]+w[4]+w[5]+w[6])
 {
     int summ=0;
     for(int i=1;i<=6;i++)
         summ+=w[i]*i;
     printf("Collection #%d:\n",yy++);
     if(summ%2==1)//如果是奇数,那么不能平分
     {
         printf("Can't be divided.\n\n");
         continue;
     }
     int sum=summ/2;
     memset(v,0,sizeof(v));
     v[0]=1;//显然0是一个人什么都不拿的情况
    for(int i=1;i<=6;i++)//枚举6种弹珠
    {
        for(int j=1;w[i];j=j<<1)//j 不断扩大两倍 
        {
            int u=i*(j<=w[i]?j:w[i]); // i*的意思是乘以弹珠价值 ,括号里面表示如果剩余的数量小于 j 那么直接取剩余数量就好 (如 100  ,我们取的是 1,2,4,8,16,32,37)
            for(int k=sum;k>=u;k--) //最大的范围到sum就好,最小范围到u ,因为我们要求k-u>0
            {
                if(v[k-u]) v[k]=1; //这句话的意思是,你把所有(已经存在的价值+ u )的情况标记为1
                if(v[sum]) break; //如果v[sum]已经存在,那么可以平分
            }
            if(v[sum]) break;
            if(j>w[i]) break; //如果 j>w[i] ,说明已经全找完了
            w[i]-=j;//剩余数量减少
        }
        if(v[sum]) break;
    }
    if(v[sum])
    printf("Can be divided.\n");
    else
      printf("Can't be divided.\n");
    printf("\n");
 }
}




基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究(Matlab代码实现)内容概要:本文围绕“基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究”,介绍了利用Matlab代码实现配电网可靠性的仿真分析方法。重点采用序贯蒙特卡洛模拟法对配电网进行长时间段的状态抽样与统计,通过模拟系统元件的故障与修复过程,评估配电网的关键可靠性指标,如系统停电频率、停电持续时间、负荷点可靠性等。该方法能够有效处理复杂网络结构与设备时序特性,提升评估精度,适用于含分布式电源、电动汽车等新型负荷接入的现代配电网。文中提供了完整的Matlab实现代码与案例分析,便于复现和扩展应用。; 适合人群:具备电力系统基础知识和Matlab编程能力的高校研究生、科研人员及电力行业技术人员,尤其适合从事配电网规划、运行与可靠性分析相关工作的人员; 使用场景及目标:①掌握序贯蒙特卡洛模拟法在电力系统可靠性评估中的基本原理与实现流程;②学习如何通过Matlab构建配电网仿真模型并进行状态转移模拟;③应用于含新能源接入的复杂配电网可靠性定量评估与优化设计; 阅读建议:建议结合文中提供的Matlab代码逐段调试运行,理解状态抽样、故障判断、修复逻辑及指标统计的具体实现方式,同时可扩展至不同网络结构或加入更多不确定性因素进行深化研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值