【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 ...
随机推荐
- php扩展开发-资源类型
资源类型在内核中的结构 //zend_list.h typedef struct _zend_rsrc_list_entry { void *ptr; int type; int refcount; ...
- openwrt(二) 配置openwrt及编译
导航 1. 配置openwrt 2. 编译openwrt 3. 错误记录 1. 配置openwrt 在openwrt的根目录下,执行make menuconfig. 这个界面我也只是了解了这两个选项而 ...
- 利用pandas和numpy计算表中每一列的均值
import numpy as np import pandas as pd df = pd.DataFrame({'var1':np.random.rand(100), #生成100个0到1之间的随 ...
- python——闰年的判断
写一个程序,判断给定年份是否为闰年. 这样定义闰年的:能被4整除但不能被100整除,或者能被400整除都是闰年. while(1): year = input("请输入一个年份,让我判断一下 ...
- HyperLedger Fabric 1.4 区块链技术定义(2.1)
区块链技术指使用点对点传输.共识机制.加密算法等技术,保证分布式数据库区块写入链中数据的一致性,达到去中心化和不可篡改的目的. 区块链就是一种特殊的分布式数据库,使用现有的各种成熟的技术, ...
- 如何将Windows live writer草稿转存到其他电脑上
ref:http://www.zhengsiwei.com/how-to-draft-windows-live-writer-archived-on-other-computers/ 在写一篇关于 ...
- Android+Handler+Thread 那些事儿
前言,才开始学安卓十几天,不料被线程挡住了前进的步伐,因为之前操作系统课程并没有认真听老师讲课,导致现在理解这些抽象的东西有些小困难.没关系,苦学之路,总会碰到坎坷,这里也给那些迷失在Android之 ...
- 1082: [SCOI2005]栅栏
链接 思路 二分+搜索+剪枝. 首先二分一个答案,表示最多可以切出x块.(一个结论:切出的一定是从较小的前x块.如果一个木材可以满足很多个需要的木材,那么切出最小的,就意味着以后再选时的机会更多.) ...
- python 发送 get post请求
GET请求: python2.7: import urllib,urllib2 url='http://192.168.199.1:8000/mainsugar/loginGET/' textmod ...
- ElasticSearch学习笔记(四)-- 分布式
1. 分布式介绍及cerebro cerebro插件 点击release下载 解压运行 访问9000端口,连接es的9200端口 2. 构建集群 新增一个节点 3. 副本与分片 再加入一个节点 4. ...