G(x)

Time Limit: 2000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 184    Accepted Submission(s): 44

Problem Description
For a binary number x with n digits (AnAn-1An-2 ... A2A1), we encode it as 
Where "" is bitwise XOR operation and "" indicates the largest integer which is not greater than x.
Due to some reasons, Mzry1992 encode his password P into G(P), and additionally, he encode P + 1 into G(P + 1) too, and write G(P) and G(P + 1) into his diary.
This story happened many years ago and now you hold the diary with these numbers in your hands. Unfortunately, some digits are unreadable now. Could you determine the values of these digits using the readable digits?
 
Input
The first line has a number T (T <= 100) , indicating the number of test cases.
For every test case, it has 2 lines of same number of digits describe G(P) and G(P + 1), In every line, it only contains 1, 0 and ?. Unreadable digits are denoted with symbol ?, The length of every line in the input is up to 105.
 
Output
For every case, you should output "Case #t: " at first, without quotes. The t is the case number starting from 1.
Then, if there is impossible to restore G(P) and G(P + 1), you should output "Impossible" in the second line.
Otherwise, if G(P) is unique, you should output restored G(P) and G(P +1) in the same format.
Otherwise, you should output "Ambiguous" and the number of possible G(P) in the second line.
The number may be very large so the answer should modulo 10^9 + 7.
 
Sample Input
3
10??
10??
0010
0110
1?01
0?01
 
Sample Output
Case #1:
Ambiguous 3
Case #2:
0010
0110
Case #3:
Impossible

Hint

In the first sample case, the three possible situations are:
1.
G(12) = 1010
G(13) = 1011
2.
G(13) = 1011
G(14) = 1001
3.
G(14) = 1001
G(15) = 1000

 
Source
 
Recommend
liuyiding
 

很容易找出规律,

然后递推,枚举就解决了。

P和P+1 其实就是差后面一点

P: XXXXX  0   1    1     1

P+1: XXX  1   0    0     0

关键点就是枚举P的第一个0的位置。左边的P和P+1是一样的。

 /* ***********************************************
Author :kuangbin
Created Time :2013/9/14 星期六 14:16:53
File Name :2013成都网络赛\1006.cpp
************************************************ */ #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; const int MAXN = ;
const int MOD = 1e9+;
char str1[MAXN], str2[MAXN];
int dp[MAXN][];
int flag[MAXN][]; char sss1[MAXN],sss2[MAXN]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int iCase = ;
scanf("%d",&T);
while(T--)
{
iCase++;
printf("Case #%d:\n",iCase);
scanf("%s%s",str1,str2);
int n = strlen(str1);
dp[][] = ;
dp[][] = ;
flag[][] = ;
flag[][] = ;
for(int i = ;i <= n;i++)
{
if(str1[i-] == '?' && str2[i-] == '?')
{
dp[i][] = dp[i-][] + dp[i-][];
dp[i][] = dp[i-][] + dp[i-][];
int tmp = flag[i-][] + flag[i-][];
if(tmp == )
flag[i][] = flag[i][] = ;
else if(tmp == )
flag[i][] = flag[i][] = ;
else flag[i][] = flag[i][] = ;
}
else if(str1[i-] == '?' || str2[i-] == '?')
{
if(str1[i-] == '' || str2[i-] =='')
{
dp[i][] = dp[i-][];
dp[i][] = dp[i-][];
flag[i][] = flag[i-][];
flag[i][] = flag[i-][];
}
else if(str1[i-] == '' || str2[i-] =='')
{
dp[i][] = dp[i-][];
dp[i][] = dp[i-][];
flag[i][] = flag[i-][];
flag[i][] = flag[i-][];
}
}
else if(str1[i-] != str2[i-])
{
dp[i][] = dp[i][] = ;
flag[i][] = flag[i][] = ;
}
else
{
if(str1[i-] =='')
{
dp[i][] = dp[i-][];
dp[i][] = dp[i-][];
flag[i][] = flag[i-][];
flag[i][] = flag[i-][]; }
else
{
dp[i][] = dp[i-][];
dp[i][] = dp[i-][];
flag[i][] = flag[i-][];
flag[i][] = flag[i-][];
}
}
if(dp[i][] >= MOD)dp[i][] -= MOD;
if(dp[i][] >= MOD)dp[i][] -= MOD;
}
int ans = ;
int flag_num = ;
int ss_id = -;
for(int i = n;i > ;i--)
{
if(i+ <= n)
{
if(str1[i+] == '' || str2[i+] == '')
break;
}
if(i+ <= n)
{
if(str1[i] == '' || str2[i] == '')
continue;
}
if(i- >= )
{
if(str1[i-] == str2[i-] && str1[i-] != '?')
continue;
}
char ch;
if(str1[i-] == str2[i-] && str1[i-] == '?')
{
ans += dp[i-][];
ans %= MOD;
ans += dp[i-][];
ans %= MOD;
flag_num += flag[i-][] + flag[i-][];
if(flag_num > )flag_num = ;
}
else
{
if(str1[i-] != '?')ch = str1[i-];
else
{
if(str2[i-] == '')ch = '';
else ch = '';
}
if(ch == '')
{
ans += dp[i-][];
ans %= MOD;
flag_num += flag[i-][];
if(flag_num > )flag_num = ;
}
else
{
ans += dp[i-][];
ans %= MOD;
flag_num += flag[i-][];
if(flag_num > )flag_num = ;
}
}
if(flag_num == && ss_id == -)ss_id = i;
}
if(flag_num == )
{
printf("Impossible\n");
continue;
}
if(flag_num > )
{
printf("Ambiguous %d\n",ans);
continue;
}
if(flag_num == )
{
sss1[n] = sss2[n] = ;
sss1[ss_id-] = '';
sss2[ss_id-] = '';
for(int i = ss_id;i < n;i++)
sss1[i] = '', sss2[i] = '';
int last = ;
for(int i = ss_id-;i >= ;i--)
{
int now;
if(flag[i+][] > )now = ;
else now = ;
sss1[i] = sss2[i] = '' + now; }
for(int i = n-; i > ;i--)
{
if(sss1[i-] ==sss1[i])sss1[i] = '';
else sss1[i] = '';
if(sss2[i-] == sss2[i])sss2[i] = '';
else sss2[i] = '';
} printf("%s\n%s\n",sss1,sss2);
continue;
}
cout<<ans<<endl; }
return ;
}

HDU 4733 G(x) (2013成都网络赛,递推)的更多相关文章

  1. HDU 4730 We Love MOE Girls (2013成都网络赛,签到水题)

    We Love MOE Girls Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. HDU 4737 A Bit Fun (2013成都网络赛)

    A Bit Fun Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. HDU 4734 F(x) (2013成都网络赛,数位DP)

    F(x) Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. HDU 4731 Minimum palindrome (2013成都网络赛,找规律构造)

    Minimum palindrome Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. HDU 4747 Mex (2013杭州网络赛1010题,线段树)

    Mex Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  6. HDU 4768 Flyer (2013长春网络赛1010题,二分)

    Flyer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. HDU 4737 A Bit Fun 2013成都 网络赛 1010

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4737 题目大意:给定一系列数,F(i,j)表示对从ai到aj连续求或运算,(i<=j)求F(i, ...

  8. 2013成都网络赛 C We Love MOE Girls(水题)

    We Love MOE Girls Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. 2013成都网络赛 J A Bit Fun(水题)

    A Bit Fun Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. AngularJS入门基础——过滤器

    在HTML中的模板绑定符号{{ }}内通过 | 符号来调用过滤器 {{ name | uppercase }}   以HTML的形式使用过滤器时,如果需要传递参数给过滤器,只要在过滤器名字后面加冒号即 ...

  2. 训练赛第二场E题 Cottage Village

    题目大意:在一条X轴上,有若干个正方形,并且保证这些正方形的中心都在X轴上,然后输入n个正方形的中心的X坐标,和正方形的边长,现在要再插入一个正方形,要求是,新插入的正方形至少要有一条边与原来的正方形 ...

  3. [转]LaTex常用数学符号整理

    转载自 http://blog.csdn.net/ying_xu/article/details/51240291 (自己保存方便查阅,侵删) 另一个网站 Markdown 添加 Latex 数学公式 ...

  4. Unity3d 常用代码

    //创建一个名为"Player"的游戏物体 //并给他添加刚体和立方体碰撞器. player=new GameObject("Player"); player. ...

  5. Mahalanobis距离(马氏距离)的“哲学”解释

    讲解教授:赵辉 (FROM : UESTC) 课程:<模式识别> 整理:PO主 基础知识: 假设空间中两点x,y,定义: 欧几里得距离, Mahalanobis距离, 不难发现,如果去掉马 ...

  6. linux中使用corntab和shell脚本自动备份nginx日志,按天备份

    编写shell脚本,实现nginx日志每天自动备份到指定文件夹! 需要的命令mv , corntab -e(定时任务),shell脚本 这里先说一下corntab: https://www.cnblo ...

  7. 【Python】Flask系列-URL和视图笔记

    1.学习目标 熟悉Flask相关知识. 熟悉web开发流程. 能独立开发Flask项目. 2.环境配置 Python虚拟环境安装 因为python的框架更新迭代太快了,有时候需要在电脑上存在一个框架的 ...

  8. mysql Keepalived 实践

    Keepalived 是一种高性能的服务器高可用或热备解决方案,Keepalived可以用来防止服务器单点故障(单点故障是指一旦某一点出现故障就会导致整个系统架构的不可用)的发生,通过配合Nginx可 ...

  9. MongoDB之pymongo

    PyMongo是什么 PyMongo是驱动程序,使python程序能够使用Mongodb数据库,使用python编写而成. 安装 环境:Ubuntu 14.04+python2.7+MongoDB 2 ...

  10. XShell 使用方法

    XShell是一款Windows下非常优秀的远程连接Linux主机的工具,是平常使用不可缺少的工具.复制和粘贴由于在linux的Shell下,Ctrl+c是中断当前指令,这个快捷键和win系统下的复制 ...