BZOJ 3107 [cqoi2013]二进制a+b (DP)

3107: [cqoi2013]二进制a+b

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 995  Solved: 444
[Submit][Status][Discuss]

Description

输入三个整数a, b, c,把它们写成无前导0的二进制整数。比如a=7, b=6, c=9,写成二进制为a=111, b=110, c=1001。接下来以位数最多的为基准,其他整数在前面添加前导0,使得a, b, c拥有相同的位数。比如在刚才的例子中,添加完前导0后为a=0111, b=0110, c=1001。最后,把a, b, c的各位进行重排,得到a’, b’, c’,使得a’+b’=c’。比如在刚才的例子中,可以这样重排:a’=0111, b’=0011, c’=1010。
你的任务是让c’最小。如果无解,输出-1。
 

Input

输入仅一行,包含三个整数a, b, c。
 

Output

 
输出仅一行,为c’的最小值。

Sample Input

7 6 9

Sample Output

10

HINT

 

a,b,c<=2^30

题解:

参考代码:dp[i][j][k][l][m]表示:第I位,第1,2,3个数分别用了几个一,m表示是否有进位:

分类讨论:

  对于m=0的情况: 

   dp[i+1][j+1][k+1][l+1][1]=min(dp[i+1][j+1][k+1][l+1][1],tmp+(1<<i+1));
    dp[i+1][j+1][k][l+1][0]=min(dp[i+1][j+1][k][l+1][0],tmp+(1<<i));
    dp[i+1][j][k+1][l+1][0]=min(dp[i+1][j][k+1][l+1][0],tmp+(1<<i));
    dp[i+1][j][k][l][0]=min(dp[i+1][j][k][l][0],tmp);
 对于m=1的情况:
    dp[i+1][j+1][k+1][l+1][1]=min(dp[i+1][j+1][k+1][l+1][1],tmp+(1<<i+1));
    dp[i+1][j][k+1][l][1]=min(dp[i+1][j][k+1][l][1],tmp+(1<<i));
    dp[i+1][j+1][k][l][1]=min(dp[i+1][j+1][k][l][1],tmp+(1<<i));
    dp[i+1][j][k][l][0]=min(dp[i+1][j][k][l][0],tmp);	
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define clr(a,b) memset(a,b,sizeof(a))
 4 typedef long long ll;
 5 const ll inf=0x3f3f3f3f3f3f3f3fll;
 6 ll a,b,c;
 7 ll la,lb,lc,len;
 8 ll dp[62][40][40][40][2];
 9 void getlen(){len=max(log2(a)+1,max(log2(b)+1,log2(c)+1));}
10 ll getnum(ll x)
11 {
12     ll sum=0;
13     for(int i=0;i<62;++i) {if( x & (1ll<<i) ) sum++; }
14     return sum;
15 }
16 void work()
17 {
18     dp[0][0][0][0][0]=0;
19     for(ll i=0;i<len;++i)
20     {
21         for(ll j=0;j<=la;++j)
22         {
23             for(ll k=0;k<=lb;++k)
24             {
25                 for(ll l=0;l<=lc;++l)
26                 {
27                      long long tmp=dp[i][j][k][l][0];//枚举最后一位不进位的情况
28                     dp[i+1][j+1][k+1][l+1][1]=min(dp[i+1][j+1][k+1][l+1][1],tmp+(1<<i+1));
29                     dp[i+1][j+1][k][l+1][0]=min(dp[i+1][j+1][k][l+1][0],tmp+(1<<i));
30                     dp[i+1][j][k+1][l+1][0]=min(dp[i+1][j][k+1][l+1][0],tmp+(1<<i));
31                     dp[i+1][j][k][l][0]=min(dp[i+1][j][k][l][0],tmp);
32                     tmp=dp[i][j][k][l][1];//枚举最后一位进位的情况
33                     dp[i+1][j+1][k+1][l+1][1]=min(dp[i+1][j+1][k+1][l+1][1],tmp+(1<<i+1));
34                     dp[i+1][j][k+1][l][1]=min(dp[i+1][j][k+1][l][1],tmp+(1<<i));
35                     dp[i+1][j+1][k][l][1]=min(dp[i+1][j+1][k][l][1],tmp+(1<<i));
36                     dp[i+1][j][k][l][0]=min(dp[i+1][j][k][l][0],tmp);    
37                 }    
38             }    
39         }    
40     } 
41     //cout<<len<<" "<<la<<" "<<lb<<" "<<lc<<endl;
42     if(dp[len][la][lb][lc][0]>=inf) printf("-1\n");
43     else printf("%d\n",dp[len][la][lb][lc][0]);
44 }
45 
46 int main()
47 {
48     scanf("%lld%lld%lld",&a,&b,&c);
49     getlen(); memset(dp,inf,sizeof dp);
50     la=getnum(a);lb=getnum(b);lc=getnum(c);
51     work();
52     return 0;
53 }
View Code

 

  

posted @ 2018-11-09 22:01  StarHai  阅读(374)  评论(0编辑  收藏  举报