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 ...
随机推荐
- bzoj 2039 & 洛谷 P1791 人员雇佣 —— 二元关系最小割
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2039 https://www.luogu.org/problemnew/show/P1791 ...
- 微服务理论之四:SOA
1.什么是SOA? SOA代表了面向服务的架构. SOA是一种使用松耦合的黑盒子服务构建业务应用的体系架构,这些服务可以通过编排连接在一起以实现特定的功能. 2.SOA特点 以下是服务的SOA的主要特 ...
- 十二:JAVA I/O
输入模式 输出模式 ⑴字节输入流:InputStream ⑵字节输出流:Output ...
- K-NN回归算法
from sklearn.datasets import load_iris import numpy as np import matplotlib.pyplot as plt iris = loa ...
- Tomcat 服务器详解
工具/原料 1.JDK:版本为jdk-7-windows-i586.exe 下载地址 http://www.oracle.com/technetwork/java/javase/download ...
- day02 秘钥生成,免密访问命令
hadoop免密登陆: 生成秘钥: ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa cat ~/.ssh/id_dsa.pub >> ~/.ssh/aut ...
- java 学习众多API和手册资源下载
这个资源包里面有jdk api 还有SSH框架的手册,数据库的手册,Jquery手册等等,还有正则表达式等, 希望可以帮助有需要的人 链接:http://希望pan.baidu.com/s/1pJ60 ...
- BOX (UVA-1587) 比较代码书写上的差距
对比一下代码的书写差距: 我的代码: #include<iostream> using namespace std; ]; ]; ]; //访问标记 bool judge(int i, i ...
- Python开发【第二篇】: 基本数据类型(一)
1. 整型 整型即整数,用 int 表示,在 Python3 中整型没有长度限制. 1.1 内置函数 1. int(num, base=None) int( ) 函数用于将字符串转换为整型 ...
- Ping++中的AlipaySDK和AlicloudUTDID冲突解决方案
今天维护一个老项目发现阿里框架冲突 问题截图: 解决方案: 去阿里文档中心 https://docs.open.alipay.com/54/104509 重新下载没有UTDID冲突的库 下载SDK解压 ...