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

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

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

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

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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string> const int maxn = +;
struct data
{
LL num;
int flag;
} a[maxn][maxn];
struct coor
{
int x,y;
LL num;
} To_number[maxn*maxn];
int len_To_number;
int n,m;
void show ()
{
for (int i=; i<=n; ++i)
{
for (int j=; j<=m; ++j)
{
if (a[i][j].flag)
printf ("%I64d ",a[i][j].num);
else printf ("? ");
}
printf ("\n");
}
return ;
}
int f;
int solve (int x,int y,LL num)
{
LL end = (LL)sqrt(num+0.5);
for (LL aa=; aa<=end; ++aa)
{
if (num%aa != ) continue;
LL bb = num/aa;//公差是aa
int k;
if (aa>=x && bb>=y)
{
for (k=; k<=len_To_number; ++k)
{
int tx = To_number[k].x - x;
int ty = To_number[k].y - y;
LL ta = aa+tx, tb = bb+ty;
if (ta*tb != To_number[k].num) break;
}
if (k == len_To_number +) return ;
}
LL taa = aa;
LL tbb = bb;
swap(taa,tbb);
//cout<<taa<<" "<<tbb<<"--"<<endl;
if (taa>=x && tbb >= y)
{
for (k=; k<=len_To_number; ++k)
{
int tx = To_number[k].x - x;
int ty = To_number[k].y - y;
LL ta = taa+tx, tb = tbb+ty;
if (ta*tb != To_number[k].num) break;
}
if (k == len_To_number +) return ;
}
}
return ;
}
void work ()
{
int allque = ;
scanf("%d%d",&n,&m);
int bx,by;
LL number;
len_To_number = ;
for (int i=; i<=n; ++i)
for (int j=; j<=m; ++j)
{
char str[];
scanf("%s",str+);
if (str[]=='?') a[i][j].flag = ;
else
{
allque = ;
a[i][j].flag=;
int lenstr = strlen(str+);
a[i][j].num=;
for (int k=; k<=lenstr; ++k)
a[i][j].num = a[i][j].num*+str[k]-'';
bx=i;
by=j;
number=a[i][j].num;
To_number[++len_To_number].num = a[i][j].num;
To_number[len_To_number].x = i;
To_number[len_To_number].y = j;
}
}
//show();
if (allque)
{
printf ("Case #%d: Yes\n",++f);
return ;
}
if (solve(bx,by,number))
{
printf ("Case #%d: Yes\n",++f);
return ;
}
else
{
printf ("Case #%d: No\n",++f);
return ;
}
} int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
scanf("%d",&t);
while (t--) work();
return ;
}

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

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

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. 51nod 1301 集合异或和——异或dp

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1301 好题!看了TJ才会. 因为是不可重集合,所以当然有前 i 个 ...

  2. DataGrid 支持字符截断显示

    DataGrid支持截断时, 需要分2部分, DataGridColumnHeader和DataGridCell. 1)创建上述2部分的ControlTemplate . 2)把其中的ContentP ...

  3. NSDictionary和NSArray

    // 字典里套数组 NSArray *array1 = @[@"huahau" , @"hehe"]; NSArray *array2 = @[@"x ...

  4. Struts2 配置项

    基础Constants struts.devMode  可选值true,false (默认false),在开发模式下,struts2的动态重新加载配置和资源文件的功能会默认生效.同时开发模式下也会提供 ...

  5. day03 hadoop的解压与配置文件的配置,还需要看视频

    拷贝hadoop1.2.1.tar.gz安装包到其他的节点上 scp -r ~/hadoop-1.2.1.tar.gz  root@node2:~/  tar -zxvf hadoop-1.2.1.t ...

  6. [Android Lint] xxx is not translated in xxx 的解决方法

    CLEAN项目即可 转自BLOG http://blog.csdn.net/feng88724/article/details/8835664

  7. [51nod1270] 数组的最大代价(简单dp)

    解题关键:先由贪心的思想得出任何一个位置只能取1或者a[i],然后dp即可. #include<bits/stdc++.h> using namespace std; typedef lo ...

  8. hbase exporter importer 导出 导入

    导出: bin/hbase org.apache.hadoop.hbase.mapreduce.Export bigtable /user/bigtable_bak/ 导入: bin/hbase or ...

  9. Shell字符串截取处理文件路径

    在生信处理流程中,从最初的fastq文件,经过分析处理后,会生成一堆的后续文件,如何在流程中合理的命名呢? 通常在批处理模式中,我们会得到多个样本*.fastq(或*.fq.*.fastq.gz.*. ...

  10. 第 2 章 Python 语言入⻔

    目录 2.1低而长的学习曲线 2.2Python的优势 2.3在你的计算机中安装Python 2.4如何运行Python程序 2.5文本编辑器 2.6寻求帮助 Python语言是一种流行的编程语言,在 ...