POJ2586:Y2K Accounting Bug

来源:互联网 发布:传奇物品数据库翻译 编辑:程序博客网 时间:2024/04/28 07:19

点击打开题目链接


Y2K Accounting Bug

Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8496 Accepted: 4198

Description

Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc. 
All what they remember is that MS Inc. posted a surplus or a deficit each month of 1999 and each month when MS Inc. posted surplus, the amount of surplus was s and each month when MS Inc. posted deficit, the deficit was d. They do not remember which or how many months posted surplus or deficit. MS Inc., unlike other companies, posts their earnings for each consecutive 5 months during a year. ACM knows that each of these 8 postings reported a deficit but they do not know how much. The chief accountant is almost sure that MS Inc. was about to post surplus for the entire year of 1999. Almost but not quite. 

Write a program, which decides whether MS Inc. suffered a deficit during 1999, or if a surplus for 1999 was possible, what is the maximum amount of surplus that they can post.

Input

Input is a sequence of lines, each containing two positive integers s and d.

Output

For each line of input, output one line containing either a single integer giving the amount of surplus for the entire year, or output Deficit if it is impossible.

Sample Input

59 237375 743200000 8496942500000 8000000

Sample Output

11628300612Deficit

Source

Waterloo local 2000.01.29


=====================================题目大意=====================================


ACM公司因为遭受Y2K Bug而丢失了部分为年度报告准备的重要数据。

他们还记得每当公司公布财政盈利时盈利的数值为S,每当公司公布财政亏损时亏损的数值为D。但是忘记了公布盈利的次数和公布亏

损的次数。

ACM公司在一年中是每5个连续月份进行一次财政公布的(1-5,2-6,。。。8-12),因此总共进行了八次财政公布。

现在已知这八次财政公布都是公布亏损的,请你计算ACM公司这一年可能的最大盈利值,如果不能盈利则输出Deficit 。


=====================================算法分析=====================================


一、公式推导:


    可证引理:对于一个仅由0和1构成的长度为Len的字符串,总存在满足条件“任取连续N个字符其中有M个字符是1”的字

             符串(其中M<=N<=Len)。且这些满足条件的字符串中1的总数的最小值为:M*[Len/N]+MAX(Len%N-(N-M),0)

            (其中[]为向下取整)。

    证明过程:不想写了!

    首先计算:满足条件“每5个连续月份的总盈利值为负数”所需的每5个连续月份的最少亏损月数Min(即使不等式D*Min>S*(5-Min)

              成立的Min的最小值)。

    类比引理:对于一个只由盈利和亏损构成的次数为12的财政情况,总存在满足条件“任取连续5次其中有Min次是亏损”的财政情况,

             且这些满足条件的财政情况中亏损的总数的最小值为:MinTimeD*[12/5]+MAX(12%5-(5-Min),0)(其中[]为向下取整)。

    因而可知:最少总亏损月数为:TotMin=Min*2+MAX(Min-3,0)。

    从而得出:最大总盈利数值为:(12-TotMin)*D-TotMin*D。


二、枚举推导:


    为了满足条件“每5个连续月份总盈利值为负数”,因此财政情况总共只有5种可能:
    1、每5个月中有1个月亏损:12个月中最少有2个月亏损(SSSSDSSSSDSS)。
    2、每5个月中有2个月亏损:12个月中最少有4个月亏损(SSSDDSSSDDSS)。
    3、每5个月中有3个月亏损:12个月中最少有6个月亏损(SSDDDSSDDDSS)。
    4、每5个月中有4个月亏损:12个月中最少有9个月亏损(SDDDDSDDDDSD)。
    5、每5个月中有4个月亏损:12个月中最少有12个月亏损(DDDDDDDDDDDD)。


三、暴力枚举:


   C++提交TLE而G++提交AC——神奇的POJ(渊哥解释说这是POJ的编译器太老了。。。)!


=======================================代码=======================================


一、公式推导。



#include<stdio.h>int S,D;int main(){while(scanf("%d%d",&S,&D)==2){int Min;for(Min=1;Min<=4;++Min) {if(D*Min>S*(5-Min)) { break; }}int TotMin=2*Min+(Min>3?Min-3:0);int TheAns=(12-TotMin)*S-TotMin*D;if(TheAns>0) {printf("%d\n",TheAns); }else { printf("Deficit\n"); }}return 0;}

二、枚举推导。



#include<stdio.h>int S,D,TotMin[5]={2,4,6,9,12};int main(){while(scanf("%d%d",&S,&D)==2){int Min;for(Min=1;Min<=4;++Min) {if(D*Min>S*(5-Min)) { break; }}int TheAns=(12-TotMin[Min-1])*S-TotMin[Min-1]*D;if(TheAns>0) {printf("%d\n",TheAns); }else { printf("Deficit\n"); }}return 0;}

三、暴力枚举。




#include<stdio.h>int S,D;int MAX(int A,int B) {return A>B?A:B;}int CntBitTrue(int Num,int LowBit,int HighBit)     {                                                  int Sum; for(Sum=0;LowBit<=HighBit;++LowBit){if(Num&(1<<LowBit)) { ++Sum; }}return Sum;}int main(){while(scanf("%d%d",&S,&D)==2){int Min;          for(Min=1;Min<=4;++Min)                    {              if(D*Min>S*(5-Min)) { break; }          }  int Ans=-1;for(int St=(1<<12)-1;St;--St)      {bool Flag=true;for(int Bit=0;Bit<8;++Bit)    {if(CntBitTrue(St,Bit,Bit+4)<Min) { Flag=false; break; }}if(Flag)                     {int TotD=CntBitTrue(St,0,11);Ans=MAX(Ans,S*(12-TotD)-D*TotD);}}if(Ans>0) {printf("%d\n",Ans); }else { printf("Deficit\n"); }}return 0;}

原创粉丝点击