UVALive 7511 L - Multiplication Table 数学模拟题,暴力
给定一副表,问其是否合法。
思路:当全部是?的时候,是合法的。
如果不是,那么,就找到一个数字,把它拆成若干个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 数学模拟题,暴力的更多相关文章
- 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 ...
- 2015 EC L - Multiplication Table
/************************************************************************* > File Name: L.cpp > ...
- 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 ...
- Codeforces 448 D. Multiplication Table 二分
题目链接:D. Multiplication Table 题意: 给出N×M的乘法矩阵要你求在这个惩罚矩阵中第k个小的元素(1 ≤ n, m ≤ 5·10^5; 1 ≤ k ≤ n·m). 题解: n ...
- cf448D Multiplication Table
D. Multiplication Table time limit per test 1 second memory limit per test 256 megabytes input stand ...
- LeetCode hard 668. Kth Smallest Number in Multiplication Table(二分答案,一次过了,好开心,哈哈哈哈)
题目:https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/description/ 668. Kth S ...
- hdu4951 Multiplication table (乘法表的奥秘)
http://acm.hdu.edu.cn/showproblem.php?pid=4951 2014多校 第八题 1008 2014 Multi-University Training Contes ...
- Codeforces Round #256 (Div. 2) D. Multiplication Table(二进制搜索)
转载请注明出处:viewmode=contents" target="_blank">http://blog.csdn.net/u012860063?viewmod ...
- 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 ...
随机推荐
- python中string和bool的转换
python中字符串"True" 和 "False"转为bool类型时, 不能通过bool(xx)强转. 注意是因为在python中,除了'& ...
- 修改MySQL的时区,涉及参数time_zone (转)
首先需要查看mysql的当前时区,用time_zone参数 mysql> show variables like '%time_zone%'; +------------------+----- ...
- 基于433MHz无线串口,多发一收解决方案
一.无线发展背景 随着科学技术的飞速发展,智能家居.智慧农业.智慧城市如雨后春笋.而这些行业的发展离不开无线的应用. 传统的有线连接不仅仅是成本高,包括布线安装.维护等也是成本巨大.并且机动性也很差, ...
- 给.sh文件添加可执行权限
有时我们运行.sh文件时会发现没有权限,具体解决方案如下 第一种:bash+执行文件 第二种:chmod命令 如果给所有人添加可执行权限:chmod a+x 文件名:如果给文件所有者添加可执行权限:c ...
- jQuery 文档操作 - html() 方法
1.转自:http://www.w3school.com.cn/jquery/manipulation_html.asp 设置所有 p 元素的内容: <html> <head> ...
- iOS开发者福利之精品源码汇总!免费下载
汇总一些看着不错的源码,有需要的朋友过来下载吧!{:4_102:} 1.用swift制作的色彩炫丽的进度条-KDCircularProgressKDCircularProgress是使用swift制作 ...
- ueditor1.4.3jsp版在上传图片报"未找到上传文件"解决方案
这是因为struts2的过滤器,解决方法是自定义一个过滤器 新建一个过滤器的类,代码: package com.filter; import java.io.IOException; import j ...
- 10、RNA-seq for DE analysis training(Mapping to assign reads to genes)
1.Goal of mapping 1)We want to assign reads to genes they were derived from 2)The result of the mapp ...
- 掌握HDFS的Java API接口访问
HDFS设计的主要目的是对海量数据进行存储,也就是说在其上能够存储很大量文件(可以存储TB级的文件).HDFS将这些文件分割之后,存储在不同的DataNode上, HDFS 提供了两种访问接口:She ...
- java虚拟机内存
-Xmx10240m:代表最大堆 -Xms10240m:代表最小堆 -Xmn5120m:代表新生代 -XXSurvivorRatio=3:代表Eden:Survivor = 3 根据Gen ...