HDU 2103 Family planning

http://acm.hdu.edu.cn/showproblem.php?pid=2103

 

Family planning

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3599    Accepted Submission(s): 952


Problem Description
As far as we known,there are so many people in this world,expecially in china.But many people like LJ always insist on that more people more power.And he often says he will burn as much babies as he could.Unfortunatly,the president XiaoHu already found LJ's extreme mind,so he have to publish a policy to control the population from keep on growing.According the fact that there are much more men than women,and some parents are rich and well educated,so the president XiaoHu made a family planning policy:
According to every parents conditions to establish a number M which means that parents can born M children at most.But once borned a boy them can't born other babies any more.If anyone break the policy will punished for 10000RMB for the first time ,and twice for the next time.For example,if LJ only allowed to born 3 babies at most,but his first baby is a boy ,but he keep on borning another 3 babies, so he will be punished for 70000RMB(10000+20000+40000) totaly.
 


 

Input
The first line of the input contains an integer T(1 <= T <= 100) which means the number of test cases.In every case first input two integers M(0<=M<=30) and N(0<=N<=30),N represent the number of babies a couple borned,then in the follow line are N binary numbers,0 represent girl,and 1 represent boy.
 


 

Output
Foreach test case you should output the total money a couple have to pay for their babies.
 


 

Sample Input
2 2 5 0 0 1 1 1 2 2 0 0
 


 

Sample Output
70000 RMB 0 RMB
 


 

Source
 


 

Recommend
xhd
 
题目大意:国家要实行计划生育,但是为了照顾到某些家庭希望能生个男孩的愿望,所以总统颁布了一条生育法令。给每个家庭一个生育孩子的限额M,表示一个家庭最多能生几个孩子,但是一旦生了男孩,即使你还没有达到生育的限额,也不能再生了。对于违规的家庭,第一次罚10000,第二次就罚20000,第三次就罚40000,以此类推,成等比数列。输入第一行是T,表示T个数据。每组数据有两行:第一行:M,N;第二行:N个孩子(1表示男孩,0表示女孩),输出这个家庭的罚款。
分析:分两段处理。用flag记录,前M个孩子中男孩的位置,有则记录位置给flag,无则为flag=M。选择n-m和n-flag中的较大值为超生数量L,通过等比数列求和公式计算,输出应为 10000*(pow(2,L)-1)。很奇怪,这里的pow(2,L)-1让我wrong了无数次,最后用for循环代替pow求解,终于AC。我在网上找了一些用pow计算最后答案的代码,也都wrong了,疑惑。。。期间,换了一种思路又写了一个代码,一次就AC了
代码如下:
(1):#include<stdio.h>
#include<string.h>
#include<math.h>
int max(int x,int y)
{
  return x>=y?x:y;
}
int main()
{
  int T,flag,n,k,i;
  int pp[100];
  __int64 ans,l,sum;
  scanf("%d",&T);
  while(T--)
  {
    flag=k;
    scanf("%d%d",&k,&n);
    memset(pp,0,sizeof(pp));
    for(i=1;i<=n;i++)
      scanf("%d",&pp[i]);
    for(i=1;i<=n;i++)
          {
      if(pp[i]==1)
            {
        flag=i;
        break;
            }
          }
          sum=10000;
          ans=0;
          l=max(n-k,n-flag);
          for(i=1;i<=l;i++)                                        //用pow计算wrong,只能用for循环求解,郁闷。。
            {
            ans+=sum;
            sum*=2;
          }
        //l=max(n-k,n-flag);
        //ans=pow(2,l)-1;
      //printf("%I64d RMB\n",10000*ans);
      //这个地方用pow计算就是wrong,到底为什么??
              printf("%I64d RMB\n",ans);
        }
  return 0;
}
(2)
#include<stdio.h>
#include<math.h>
int main()
{
  int T,n,k,i,w;
  __int64 ans,sum;
  int flag;
  scanf("%d",&T);
  while(T--)
  {
    scanf("%d%d",&k,&n);
    ans=flag=0;
    sum=10000;
    for(i=1;i<=n;i++)
    {
        scanf("%d",&w);
        if(i>k||flag==1)                                              //这里也只能用for循环。。不敢用pow了。。
         
        ans+=sum;
        sum*=2;
        }
          if(w==1) flag=1;                                        //这里的if判断必须放在下面,否则超生数量会+1
     
      printf("%I64d RMB\n",ans);
  }
  return 0;
}
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值