给定一副表,问其是否合法。

思路:当全部是?的时候,是合法的。

如果不是,那么,就找到一个数字,把它拆成若干个a*b的形式,去判断其它点是否合法即可。

拆分数字的时候,只需要枚举到sqrt(n),因为肯定是两个小于sqrt n的数相乘得到的结果。

比如6=1*6 6=2*3 注意分解后,考虑调换顺序是否合法即可。

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. using namespace std;
  7. #define inf (0x3f3f3f3f)
  8. typedef long long int LL;
  9.  
  10. #include <iostream>
  11. #include <sstream>
  12. #include <vector>
  13. #include <set>
  14. #include <map>
  15. #include <queue>
  16. #include <string>
  17.  
  18. const int maxn = +;
  19. struct data
  20. {
  21. LL num;
  22. int flag;
  23. } a[maxn][maxn];
  24. struct coor
  25. {
  26. int x,y;
  27. LL num;
  28. } To_number[maxn*maxn];
  29. int len_To_number;
  30. int n,m;
  31. void show ()
  32. {
  33. for (int i=; i<=n; ++i)
  34. {
  35. for (int j=; j<=m; ++j)
  36. {
  37. if (a[i][j].flag)
  38. printf ("%I64d ",a[i][j].num);
  39. else printf ("? ");
  40. }
  41. printf ("\n");
  42. }
  43. return ;
  44. }
  45. int f;
  46. int solve (int x,int y,LL num)
  47. {
  48. LL end = (LL)sqrt(num+0.5);
  49. for (LL aa=; aa<=end; ++aa)
  50. {
  51. if (num%aa != ) continue;
  52. LL bb = num/aa;//公差是aa
  53. int k;
  54. if (aa>=x && bb>=y)
  55. {
  56. for (k=; k<=len_To_number; ++k)
  57. {
  58. int tx = To_number[k].x - x;
  59. int ty = To_number[k].y - y;
  60. LL ta = aa+tx, tb = bb+ty;
  61. if (ta*tb != To_number[k].num) break;
  62. }
  63. if (k == len_To_number +) return ;
  64. }
  65. LL taa = aa;
  66. LL tbb = bb;
  67. swap(taa,tbb);
  68. //cout<<taa<<" "<<tbb<<"--"<<endl;
  69. if (taa>=x && tbb >= y)
  70. {
  71. for (k=; k<=len_To_number; ++k)
  72. {
  73. int tx = To_number[k].x - x;
  74. int ty = To_number[k].y - y;
  75. LL ta = taa+tx, tb = tbb+ty;
  76. if (ta*tb != To_number[k].num) break;
  77. }
  78. if (k == len_To_number +) return ;
  79. }
  80. }
  81. return ;
  82. }
  83. void work ()
  84. {
  85. int allque = ;
  86. scanf("%d%d",&n,&m);
  87. int bx,by;
  88. LL number;
  89. len_To_number = ;
  90. for (int i=; i<=n; ++i)
  91. for (int j=; j<=m; ++j)
  92. {
  93. char str[];
  94. scanf("%s",str+);
  95. if (str[]=='?') a[i][j].flag = ;
  96. else
  97. {
  98. allque = ;
  99. a[i][j].flag=;
  100. int lenstr = strlen(str+);
  101. a[i][j].num=;
  102. for (int k=; k<=lenstr; ++k)
  103. a[i][j].num = a[i][j].num*+str[k]-'';
  104. bx=i;
  105. by=j;
  106. number=a[i][j].num;
  107. To_number[++len_To_number].num = a[i][j].num;
  108. To_number[len_To_number].x = i;
  109. To_number[len_To_number].y = j;
  110. }
  111. }
  112. //show();
  113. if (allque)
  114. {
  115. printf ("Case #%d: Yes\n",++f);
  116. return ;
  117. }
  118. if (solve(bx,by,number))
  119. {
  120. printf ("Case #%d: Yes\n",++f);
  121. return ;
  122. }
  123. else
  124. {
  125. printf ("Case #%d: No\n",++f);
  126. return ;
  127. }
  128. }
  129.  
  130. int main()
  131. {
  132. #ifdef local
  133. freopen("data.txt","r",stdin);
  134. #endif
  135. int t;
  136. scanf("%d",&t);
  137. while (t--) work();
  138. return ;
  139. }

另外,枚举两个点的话,满足这两个点的解,就是唯一的解。也可以枚举两个点,然后确定唯一的一个解,再去比较整个地图。

不过也没什么必要。速度差不多。因为很少这样的例子,使得枚举的时候有很多个数字是满足的。

UVALive 7511 L - Multiplication Table 数学模拟题,暴力的更多相关文章

  1. Codeforces Codeforces Round #319 (Div. 2) A. Multiplication Table 水题

    A. Multiplication Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/57 ...

  2. 2015 EC L - Multiplication Table

    /************************************************************************* > File Name: L.cpp > ...

  3. HDU 4951 Multiplication table 阅读题

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4951 题意:给一个P进制的乘法表.行和列分别代表0~p-1,第i行第j*2+1和第j*2+2列代表的是第i ...

  4. Codeforces 448 D. Multiplication Table 二分

    题目链接:D. Multiplication Table 题意: 给出N×M的乘法矩阵要你求在这个惩罚矩阵中第k个小的元素(1 ≤ n, m ≤ 5·10^5; 1 ≤ k ≤ n·m). 题解: n ...

  5. cf448D Multiplication Table

    D. Multiplication Table time limit per test 1 second memory limit per test 256 megabytes input stand ...

  6. LeetCode hard 668. Kth Smallest Number in Multiplication Table(二分答案,一次过了,好开心,哈哈哈哈)

    题目:https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/description/ 668. Kth S ...

  7. hdu4951 Multiplication table (乘法表的奥秘)

    http://acm.hdu.edu.cn/showproblem.php?pid=4951 2014多校 第八题 1008 2014 Multi-University Training Contes ...

  8. Codeforces Round #256 (Div. 2) D. Multiplication Table(二进制搜索)

    转载请注明出处:viewmode=contents" target="_blank">http://blog.csdn.net/u012860063?viewmod ...

  9. Codeforces Round #256 (Div. 2) D. Multiplication Table 二分法

     D. Multiplication Table time limit per test 1 second memory limit per test 256 megabytes input st ...

随机推荐

  1. bzoj 2039 & 洛谷 P1791 人员雇佣 —— 二元关系最小割

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2039 https://www.luogu.org/problemnew/show/P1791 ...

  2. 微服务理论之四:SOA

    1.什么是SOA? SOA代表了面向服务的架构. SOA是一种使用松耦合的黑盒子服务构建业务应用的体系架构,这些服务可以通过编排连接在一起以实现特定的功能. 2.SOA特点 以下是服务的SOA的主要特 ...

  3. 十二:JAVA I/O

     输入模式                                                          输出模式 ⑴字节输入流:InputStream ⑵字节输出流:Output ...

  4. K-NN回归算法

    from sklearn.datasets import load_iris import numpy as np import matplotlib.pyplot as plt iris = loa ...

  5. Tomcat 服务器详解

    工具/原料 1.JDK:版本为jdk-7-windows-i586.exe  下载地址   http://www.oracle.com/technetwork/java/javase/download ...

  6. day02 秘钥生成,免密访问命令

    hadoop免密登陆: 生成秘钥: ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa cat ~/.ssh/id_dsa.pub >> ~/.ssh/aut ...

  7. java 学习众多API和手册资源下载

    这个资源包里面有jdk api 还有SSH框架的手册,数据库的手册,Jquery手册等等,还有正则表达式等, 希望可以帮助有需要的人 链接:http://希望pan.baidu.com/s/1pJ60 ...

  8. BOX (UVA-1587) 比较代码书写上的差距

    对比一下代码的书写差距: 我的代码: #include<iostream> using namespace std; ]; ]; ]; //访问标记 bool judge(int i, i ...

  9. Python开发【第二篇】: 基本数据类型(一)

    1. 整型   整型即整数,用 int 表示,在 Python3 中整型没有长度限制. 1.1 内置函数   1. int(num, base=None)   int( ) 函数用于将字符串转换为整型 ...

  10. Ping++中的AlipaySDK和AlicloudUTDID冲突解决方案

    今天维护一个老项目发现阿里框架冲突 问题截图: 解决方案: 去阿里文档中心 https://docs.open.alipay.com/54/104509 重新下载没有UTDID冲突的库 下载SDK解压 ...