【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 ...
随机推荐
- vue 改变我们插值的符号{{}}改为${}
delimiters的作用是改变我们插值的符号.Vue默认的插值是双大括号{{}}.但有时我们会有需求更改这个插值的形式. delimiters:['${','}'] 现在我们的插值形式就变成了${} ...
- yii2 url 美化参数
所谓的url参数美化就是将冗长的字符串 进行正则替换 yii2 框架的url参数美化完成需要完成两个阶段 第一个阶段分apache和nginx 两种的配置 apache :1.1 必须开启rewrit ...
- Apache2服务配置ubuntu16.04+django1.11
话不多说直接上步骤 环境 Ubuntu 16.04 Python 3.5.2 Django 1.11 Apache 2.4 1.Apache2安装 sudo apt-get install apach ...
- qt5.10.1编译记录
qt版本更新比较快,不知道选哪个版本合适,故选择一个较新版本的. 平台imx6 + linux4.1.16 + qt5.10.1 采用明远智睿提供的编译器:fsl-imx-fb-g ...
- Git Pro Book
目录 2nd Edition (2014) Switch to 1st Edition Download Ebook The entire Pro Git book, written by Scott ...
- 第一章:Hello, World!
感谢作者 –> 原文链接 本文翻译自The Flask Mega-Tutorial Part I: Hello, World! 一趟愉快的学习之旅即将开始,跟随它你将学会用Python和Flas ...
- JAVA-数组或集合
哈哈,今天我们来讲解一下有关于一些数组 或者是集合的知识点 1.ArrayList,LinkedList,Vector的区别 ArrayList,LinkedList,Vector都是实现List接口 ...
- 20145202马超《java程序设计》第一周学习总结
这两天的学习让我对java有了初步的了解. 1.java是SUN公司推出的面相网络的编程语言. 特点:完全面向对象,与平台无关,跨平台性(例如c++只能在windows上执行,然而java并没有这些限 ...
- 2-安装linux7
1.操作系统简介 操作系统: 桌面操作系统 redhat fedora slackware ubuntu debian suse linux centos 服务器操作系统 linux redhat s ...
- 3. 与服务器对话:理解 HTTP 协议
0.服务器与本地交换机制 2.详解HTtp服务 (1)与服务器对话的流程 (2)Reque 请求 (3)Response 响应 200 成功 404 没有网页 (4)Get/Post区别 get查询数 ...