D. Multiplication Table
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Bizon the Champion isn't just charming, he also is very smart.

While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted ann × m multiplication
table, where the element on the intersection of the i-th row and j-th
column equals i·j (the rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table is the k-th
largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success?

Consider the given multiplication table. If you write out all n·m numbers from the table in the non-decreasing order, then the k-th
number you write out is called the k-th largest number.

Input

The single line contains integers nm and k (1 ≤ n, m ≤ 5·105; 1 ≤ k ≤ n·m).

Output

Print the k-th largest number in a n × m multiplication
table.

Sample test(s)
input
2 2 2
output
2
input
2 3 4
output
3
input
1 10 5
output
5
Note

A 2 × 3 multiplication table looks like this:

1 2 3
2 4 6

唉这么水的题竟然final test能wa

题意是已知一个固定的矩阵,其中第i行第j列是i*j,然后问在n*m的这样的矩阵中第k大是多少

首先矩阵是固定的,矩阵中数据的规模是2500e。这样的大小对于一般递推太大了些

但是二分+判定的做法还是很好想的

我们每次二分一个可能的第k大数,然后第i行所有小于等于它的数的个数是min(mid/i,m)(因为尽管一排中可能最多有mid/i个数,但是题目已经限定是n*m,直接加mid/i显然不对,我就是这里一开始第五个点wa),用这样的办法统计出所有小于它的数,就很好判定了

但是仅仅是这样还是不行。所以我final test wa了。为什么不行呢?注意到矩阵中并不包含所有正整数,并且显然除了第一行能凑出1到n、第一列可以凑出1到m之外,只有合数可以存在于矩阵中。但是由于二分一旦找到sum加起来等于k的时候就退出,于是会出现这样斯巴达的情况:

对于3*3的矩阵,我们求第8大:

1 2 3

2 4 6

3 6 9

我们二分1到9

第一次:mid=5,sum=6

第二次:mid=7,sum=8

此时sum=k,就应该退出了。但是显然7没有出现在矩阵中!所以问题出现了:有时会二分到不在矩阵中的数。

怎么处理呢?

我们在二分的时候加一个prime()函数,用来判定质数。如果mid<=n或者mid<=m或者mid是合数的时候,它才可能出现在矩阵中。

那么每次做prime()效率是sqrt(n*m)就是50w级的,每行搜过去更新sum是O(m)也是50w的,最多二分log(2500e)大概3、40次,是不会爆的

#include<bits/stdc++.h>
#include<iostream>
#define LL long long
using namespace std;
LL n,m,k,l,r,ans;
inline bool prime(LL x)
{
if (x<2) return 0;
for (int i=2;i*i<=x;i++)
if (x%i) return 0;
return 1;
}
int main()
{
scanf("%lld%lld%lld",&n,&m,&k);
l=1;r=m*n;
while (l<=r)
{
LL mid=(l+r)>>1,sum=0;
bool mark=mid<=n||mid<=m||prime(mid);
for (int j=1;j<=n;j++)
{
sum+=min(mid/j,m);
}
if (sum==k&&mark)
{
printf("%lld",mid);
return 0;
}
else
{
if (sum<k) l=mid+1;
else
{
r=mid-1;
ans=mid;
}
}
}
printf("%lld",ans);
}

cf448D Multiplication Table的更多相关文章

  1. 洛谷 CF448D Multiplication Table

    目录 题目 思路 \(Code\) 题目 CF448D Multiplication Table 思路 二分答案.这个矩阵的每一排都是递增的,所以二分\(ans\),去计算有多少个数等于\(ans\) ...

  2. cf448D Multiplication Table 二分

    题目:http://codeforces.com/problemset/problem/448/D 题意:给出n,m,k,即在一个n*m的二维数组中找第k大的数,第i行第j列的数的值为i*j. 思路: ...

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

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

  4. 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 ...

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

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

  6. 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 ...

  7. Codeforces 448 D. Multiplication Table

    二分法判断答案 D. Multiplication Table time limit per test 1 second memory limit per test 256 megabytes inp ...

  8. Codeforces 448 D. Multiplication Table 二分

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

  9. [LeetCode] Kth Smallest Number in Multiplication Table 乘法表中的第K小的数字

    Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...

随机推荐

  1. DateTime字段控件值显示短格式的做法

    后台取dateTime格式,前台格式化就好了 <input type="text" name="txtPartyTime" id="txtPar ...

  2. OpenCV的矩阵合并方法

    有的时候我们需要将几个矩阵按行或者按列进行合并成一个大矩阵,这在Matlab里面非常的简单,但在OpenCV里面并没有这样的方法,现在我在OpenCV的源码里面发现合并矩阵的方法,分享给大家. A = ...

  3. Fence Repair (POJ 3253)

    农夫约翰为了修理栅栏,要将一块很长的木板切割成N块.准备切成的木板长度为L1.L2.L3...LN,未切割前的木板长度恰好为切割后木板长度的总和.每次切断木板时,需要的开销为这块木板的长度.例如长度为 ...

  4. 第30讲 UI组件之 GridView组件

    第30讲 UI组件之 GridView组件 1.网格布局组件GridView GridView是一个ViewGroup(布局控件),可使用表格的方式显示组件,可滚动的控件.一般用于显示多张图片,比如实 ...

  5. eclispe远程调试tomcat

    在eclispe中新建web应用,名字叫webtest.里面只有一个HelloServlet.Web.xml配置如下. 修改tomcat的启动脚本startup.bat.复制startup.bat为s ...

  6. 通过BulkLoad的方式快速导入海量数据

    摘要 加载数据到HBase的方式有多种,通过HBase API导入或命令行导入或使用第三方(如sqoop)来导入或使用MR来批量导入(耗费磁盘I/O,容易在导入的过程使节点宕机),但是这些方式不是慢就 ...

  7. World Wind Java开发之十五——载入三维模型

    之前的一篇博客是关于载入粗三维模型的,见http://blog.csdn.net/giser_whu/article/details/43452703,这个地方还存在着不能载入纹理的问题,一直没呢解决 ...

  8. 针对淡入淡出的定时轮播效果js

    如果不使用jquery的fadeIn和fadeOut的接口和不适用animate情况下,如果要做用js实现淡入淡出轮播效果,我所想到的办法就是使用css3新特性transition(注意好兼容性). ...

  9. 除去内容中的HTML代码方法

    显示内容时,需要截取部分,而不要全部显示.在截取时,会出现这样的情况: 截取一定量的字符串后,可能会把未关闭的表格HTML代码留下来,最終导致界面受影响, 下面的是C#解决办法: public str ...

  10. Java数据结构漫谈-LinkedList

    同样是List的数据结构,LinkedList是使用了前后指针,指明节点的方式来表示链表的,这与之前介绍的ArrayList http://www.cnblogs.com/yakovchang/p/j ...