【ZOJ4061】Magic Multiplication(构造)
题意:定义一个新运算为两个数A,B上每一位相乘,然后顺次接在一起,现在给定结果C和原来两个数字的长度,要求恢复成原来的数字A,B
若有多解输出A字典序最小的,A相同输出B字典序最小的,无解输出Impossible
n,m<=2e5,sigma lenc<=2e6
思路:实际上只需要枚举A的第一位就行了,因为给定一个1-9的数字和接下去的1-2位结果,构造方案一定是唯一的
因为题目要求A和B都没有前导0,根据枚举的A[1]推出B,再根据B[1]推出剩余的A,暴力检验是否与C相等
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair
#define N 210000
#define M 51
#define MOD 1000000007
#define eps 1e-8
#define pi acos(-1)
#define oo 1010000000 char c[N];
int a[N],b[N],n,m,now,len; int calcB()
{
for(int i=;i<=m;i++)
{
int t=c[now++]-'';
if(a[]>t&&t!=) t=t*+c[now++]-'';
if(t%a[]==&&(t/a[])<) b[i]=t/a[];
else return ;
}
return ;
} int calcA()
{
for(int i=;i<=n;i++)
{
int t=c[now++]-'';
if(b[]>t&&t!=) t=t*+c[now++]-'';
if(t%b[]==&&(t/b[])<) a[i]=t/b[];
else return ;
for(int j=;j<=m;j++)
{
int t=c[now++]-'';
if(a[i]>t&&t!=) t=t*+c[now++]-'';
if(a[i]*b[j]!=t) return ;
}
}
return ;
} int solve()
{
int one=c[]-'';
int two=one*+c[]-'';
for(int i=;i<=;i++)
if(one%i==)
{
now=;
a[]=i;
if(calcB()&&calcA()&&now==len+) return ;
} for(int i=;i<=;i++)
if(two%i==&&two/i<)
{
now=;
a[]=i;
if(calcB()&&calcA()&&now==len+) return ;
}
return ;
} int main()
{
int cas;
scanf("%d",&cas);
for(int v=;v<=cas;v++)
{
scanf("%d%d",&n,&m);
scanf("%s",c+);
len=strlen(c+);
if(solve())
{
for(int i=;i<=n;i++) printf("%d",a[i]);
printf(" ");
for(int i=;i<=m;i++) printf("%d",b[i]);
printf("\n");
}
else printf("Impossible\n");
}
return ;
}
【ZOJ4061】Magic Multiplication(构造)的更多相关文章
- ACM-ICPC 2018 青岛赛区现场赛 D. Magic Multiplication && ZOJ 4061 (思维+构造)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4061 题意:定义一个长度为 n 的序列 a1,a2,..,an ...
- C. Magic Grid 构造矩阵
C. Magic Grid time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 【Zoj 4061】Magic Multiplication
[链接] 我是链接,点我呀:) [题意] [题解] /* for a[1] from 1~9 1*1=1 2*1=2 3*1=3 1*2=2 2*2=4 3*2=6 1*3=3 2*3=6 3*3=9 ...
- 2018 ACM-ICPC 区域赛(青岛站)题解整理
题目链接 C - Flippy Sequence(组合数学+分类讨论) 两区间异或一下,分段考虑,如果全为0则任选两相同区间,答案为$C_{n+1}^{2}=\frac{n(n+1)}{2}$,只有一 ...
- python 3.3.2 爬虫记录
网络上大部分关于python爬虫的介绍以及代码讲解,都用的是python2.7或以下版本,用python3.x版本的甚少. 在python3.3.2版本中,没有urllib2这个库,也没有cookie ...
- python实现域账号登陆
需求:公司的网路比较变态,每天到了24点自动断开,为了避免一台测试机断网,用python做了一个自动登录 原理:时间到了24点的时候,每隔10秒检测是否可以ping通www.baidu.com,如果p ...
- 构造 - SGU 109 Magic of David Copperfield II
Magic of David Copperfield II Problem's Link Mean: 略 analyse: 若i+j为奇数则称(i,j)为奇格,否则称(i+j)为偶格,显然每一次报数后 ...
- CodeForces - 710C Magic Odd Square(奇数和幻方构造)
Magic Odd Square Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, c ...
- CF1110E Magic Stones(构造题)
这场CF怎么这么多构造题…… 题目链接:CF原网 洛谷 题目大意:给定两个长度为 $n$ 的序列 $c$ 和 $t$.每次我们可以对 $c_i(2\le i<n)$ 进行一次操作,也就是把 $c ...
随机推荐
- python3 练习题100例 (十七)四位车号问题
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'Fan Lijun' import math for i in range(1 ...
- 笔记--tslib 编译
tslib 是qt启动时的一个触屏校正检验程序. 它的配置以及编译比较简单. 第一步, 下载tslib源码包: http://download.csdn.net/detail/MKNDG/329156 ...
- HDU 2222 AC自动机(模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- python——获取数据类型
在python中,可使用type()和isinstance()内置函数获取数据类型 如: (1)type()的使用方法: >>> a = '230' >> ...
- [WorldFinal 2012E]Infiltration(dfs+图论)
Description 题意:给定一个点数为n的竞赛图,求图的最小支配集 n<=75 Solution 如果将竞赛图的一个点删去,这个图还是竞赛图 而竞赛图每个点相连的边数为(n-1),那么删去 ...
- [回文树][BZOJ2160][国家集训队]拉拉队排练
题面 Description 艾利斯顿商学院篮球队要参加一年一度的市篮球比赛了.拉拉队是篮球比赛的一个看点,好的拉拉队往往能帮助球队增加士气,赢得最终的比赛.所以作为拉拉队队长的楚雨荨同学知道,帮助篮 ...
- PHP.17-文本式留言板
文本式留言板 思路: 1.首页:index.php 添加/查看信息界面 单纯的表单页面,注意留言内容为文本域<textarea> 2.添加信息页面:doAdd.php 1.获取要添加的留 ...
- PHP代码审计3-SQL注入,CSRF,动态函数执行与匿名函数执行,unserialize 反序列化漏洞,变量覆盖,文件管理,文件上传
SQL注入 审计语句 [输入参数] SELECT,DELETE,UPDATE,INSERT 防御 转义: 1.开启gpc:判断解析用户提示的数据 2.mysql_real_escape_string( ...
- Linux 安装github并配置ssh
首先,你得有个github帐号. 1.用apt-get install git的方式安装git test@er:/$ sudo add-apt-repository ppa:git-core/ppa ...
- Hibernate---架构
Hibernate 架构是分层的,作为数据访问层,你不必知道底层 API .Hibernate 利用数据库以及配置数据来为应用程序提供持续性服务(以及持续性对象). 下面是一个非常高水平的 Hiber ...