题意:定义一个新运算为两个数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相等

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<string>
  4. #include<cmath>
  5. #include<iostream>
  6. #include<algorithm>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<vector>
  11. using namespace std;
  12. typedef long long ll;
  13. typedef unsigned int uint;
  14. typedef unsigned long long ull;
  15. typedef pair<int,int> PII;
  16. typedef vector<int> VI;
  17. #define fi first
  18. #define se second
  19. #define MP make_pair
  20. #define N 210000
  21. #define M 51
  22. #define MOD 1000000007
  23. #define eps 1e-8
  24. #define pi acos(-1)
  25. #define oo 1010000000
  26.  
  27. char c[N];
  28. int a[N],b[N],n,m,now,len;
  29.  
  30. int calcB()
  31. {
  32. for(int i=;i<=m;i++)
  33. {
  34. int t=c[now++]-'';
  35. if(a[]>t&&t!=) t=t*+c[now++]-'';
  36. if(t%a[]==&&(t/a[])<) b[i]=t/a[];
  37. else return ;
  38. }
  39. return ;
  40. }
  41.  
  42. int calcA()
  43. {
  44. for(int i=;i<=n;i++)
  45. {
  46. int t=c[now++]-'';
  47. if(b[]>t&&t!=) t=t*+c[now++]-'';
  48. if(t%b[]==&&(t/b[])<) a[i]=t/b[];
  49. else return ;
  50. for(int j=;j<=m;j++)
  51. {
  52. int t=c[now++]-'';
  53. if(a[i]>t&&t!=) t=t*+c[now++]-'';
  54. if(a[i]*b[j]!=t) return ;
  55. }
  56. }
  57. return ;
  58. }
  59.  
  60. int solve()
  61. {
  62. int one=c[]-'';
  63. int two=one*+c[]-'';
  64. for(int i=;i<=;i++)
  65. if(one%i==)
  66. {
  67. now=;
  68. a[]=i;
  69. if(calcB()&&calcA()&&now==len+) return ;
  70. }
  71.  
  72. for(int i=;i<=;i++)
  73. if(two%i==&&two/i<)
  74. {
  75. now=;
  76. a[]=i;
  77. if(calcB()&&calcA()&&now==len+) return ;
  78. }
  79. return ;
  80. }
  81.  
  82. int main()
  83. {
  84. int cas;
  85. scanf("%d",&cas);
  86. for(int v=;v<=cas;v++)
  87. {
  88. scanf("%d%d",&n,&m);
  89. scanf("%s",c+);
  90. len=strlen(c+);
  91. if(solve())
  92. {
  93. for(int i=;i<=n;i++) printf("%d",a[i]);
  94. printf(" ");
  95. for(int i=;i<=m;i++) printf("%d",b[i]);
  96. printf("\n");
  97. }
  98. else printf("Impossible\n");
  99. }
  100. return ;
  101. }

【ZOJ4061】Magic Multiplication(构造)的更多相关文章

  1. ACM-ICPC 2018 青岛赛区现场赛 D. Magic Multiplication && ZOJ 4061 (思维+构造)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4061 题意:定义一个长度为 n 的序列 a1,a2,..,an ...

  2. C. Magic Grid 构造矩阵

    C. Magic Grid time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. 【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 ...

  4. 2018 ACM-ICPC 区域赛(青岛站)题解整理

    题目链接 C - Flippy Sequence(组合数学+分类讨论) 两区间异或一下,分段考虑,如果全为0则任选两相同区间,答案为$C_{n+1}^{2}=\frac{n(n+1)}{2}$,只有一 ...

  5. python 3.3.2 爬虫记录

    网络上大部分关于python爬虫的介绍以及代码讲解,都用的是python2.7或以下版本,用python3.x版本的甚少. 在python3.3.2版本中,没有urllib2这个库,也没有cookie ...

  6. python实现域账号登陆

    需求:公司的网路比较变态,每天到了24点自动断开,为了避免一台测试机断网,用python做了一个自动登录 原理:时间到了24点的时候,每隔10秒检测是否可以ping通www.baidu.com,如果p ...

  7. 构造 - SGU 109 Magic of David Copperfield II

    Magic of David Copperfield II Problem's Link Mean: 略 analyse: 若i+j为奇数则称(i,j)为奇格,否则称(i+j)为偶格,显然每一次报数后 ...

  8. 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 ...

  9. CF1110E Magic Stones(构造题)

    这场CF怎么这么多构造题…… 题目链接:CF原网 洛谷 题目大意:给定两个长度为 $n$ 的序列 $c$ 和 $t$.每次我们可以对 $c_i(2\le i<n)$ 进行一次操作,也就是把 $c ...

随机推荐

  1. vue 改变我们插值的符号{{}}改为${}

    delimiters的作用是改变我们插值的符号.Vue默认的插值是双大括号{{}}.但有时我们会有需求更改这个插值的形式. delimiters:['${','}'] 现在我们的插值形式就变成了${} ...

  2. yii2 url 美化参数

    所谓的url参数美化就是将冗长的字符串 进行正则替换 yii2 框架的url参数美化完成需要完成两个阶段 第一个阶段分apache和nginx 两种的配置 apache :1.1 必须开启rewrit ...

  3. 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 ...

  4. qt5.10.1编译记录

    qt版本更新比较快,不知道选哪个版本合适,故选择一个较新版本的. 平台imx6    +    linux4.1.16   +   qt5.10.1 采用明远智睿提供的编译器:fsl-imx-fb-g ...

  5. Git Pro Book

    目录 2nd Edition (2014) Switch to 1st Edition Download Ebook The entire Pro Git book, written by Scott ...

  6. 第一章:Hello, World!

    感谢作者 –> 原文链接 本文翻译自The Flask Mega-Tutorial Part I: Hello, World! 一趟愉快的学习之旅即将开始,跟随它你将学会用Python和Flas ...

  7. JAVA-数组或集合

    哈哈,今天我们来讲解一下有关于一些数组 或者是集合的知识点 1.ArrayList,LinkedList,Vector的区别 ArrayList,LinkedList,Vector都是实现List接口 ...

  8. 20145202马超《java程序设计》第一周学习总结

    这两天的学习让我对java有了初步的了解. 1.java是SUN公司推出的面相网络的编程语言. 特点:完全面向对象,与平台无关,跨平台性(例如c++只能在windows上执行,然而java并没有这些限 ...

  9. 2-安装linux7

    1.操作系统简介 操作系统: 桌面操作系统 redhat fedora slackware ubuntu debian suse linux centos 服务器操作系统 linux redhat s ...

  10. 3. 与服务器对话:理解 HTTP 协议

    0.服务器与本地交换机制 2.详解HTtp服务 (1)与服务器对话的流程 (2)Reque 请求 (3)Response 响应 200 成功 404 没有网页 (4)Get/Post区别 get查询数 ...