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 ...
随机推荐
- Sublime Text 全程指南(转载)
摘要(Abstract) 本文系统全面的介绍了Sublime Text,旨在成为最优秀的Sublime Text中文教程. 更新记录 2014/09/27:完成初稿 2014/09/28: 更正打开控 ...
- 通用RowMapper封装查询结果到自定义类中
package object; import java.lang.reflect.Field;import java.sql.ResultSet;import java.sql.SQLExceptio ...
- 解决mysql 客户端连接问题
问题:在linux 上安装了mysql服务端,使用客户端连接时报错信息为:ERROR 1045 (28000): Access denied for user 'root'@'localhos ...
- MyBatis动态传入表名,字段名参数的解决办法---statementType用法
statementType="STATEMENT" 要实现动态传入表名.列名,需要做如下修改 添加属性statementType="STATEMENT" 同时s ...
- Code:template
ylbtech-Code: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtech.cn ...
- Python知识点: os.popen
用例:f = os.popen("%s %s %s" % ("pkg-config", " ".join(args), mod)) pope ...
- springMVC绑定json参数之二(2.2.1)
二.springmvc 接收不同格式的json字符串 2.扫盲完了继续测试springmvc 接收不同格式的json字符串 1).格式一:json简单数组对象 前台两种传递方式: 方式一(需要拼接js ...
- hadoop自己写的最高温度程序源码
package com.teset; import java.io.IOException; import java.util.StringTokenizer; import org.apache.h ...
- JavaScript的内部对象
JavaScript的内部对象 按创建方式不同分为:使用变量声明的隐性对象,使用new创建的显性对象 隐性对象 在赋值和声明后就是一个隐性对象,隐性对象不支持prototype属性,也无法随意扩展对象 ...
- 5.6 安装SqlDeveloper
首先,将安装包准备好: 打开终端: 这样,sqldeveloper就安装完毕了. 在Ubuntu中搜索,sql,会出现: 点击,可以进入: