[恢]hdu 1239
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1239
題意:給m、a、b。求一對(duì)素?cái)?shù)p,q(p<q)使得p*q<=m且p/q >= a/b。若有多對(duì),輸出p*q最大的一對(duì)。
mark:剛開(kāi)題看了半天,才看明白啥意思。
看到m是10w,然后case是2000組,一下懵了,以為不能枚舉。后來(lái)冷靜分析下,其實(shí)可以。
以下是幾個(gè)比較重要的結(jié)論&分析。
1)若m' = p*q,則必不存在另外一對(duì)素?cái)?shù)p',q'使得m' = p' * q'。
2) 若p固定,q要滿足:1、p*q<=m;2、p/q>=a/b。則可以推出q是滿足小于min(m/p, p*b/a)的素?cái)?shù),而因?yàn)橐髉*q最大,所以q應(yīng)該是滿足條件的最大素?cái)?shù)。
3) 因?yàn)閜>=q而且要求p*q <=m,所以p <=sqrt(m)。
因此,我們只需要從2到sqrt(m)枚舉每個(gè)素?cái)?shù)p,計(jì)算出相應(yīng)的q。m是10w,開(kāi)根號(hào)只有300多,非常小,素?cái)?shù)就更少了。即使是2000*300也是可以接受的。
代碼:
# include <stdio.h># include <stdlib.h>
# include <math.h>
int IsPrime[50010] = {1, 1} ;
int Primes[10000] ;
int pcnt = 0 ;
void init()
{
int i, j ;
for (i = 2 ; i <= 50003 ; i++)
{
if (!IsPrime[i])
{
Primes[pcnt++] = i ;
for (j = 2*i ; j <= 50003 ; j+=i)
IsPrime[j] = 1 ;
}
}
}
int min(int a, int b){return a<b?a:b;}
int main ()
{
int m, a, b, sqrtm ;
int p, q, ansp, ansq ;
init () ;
while (~scanf ("%d%d%d", &m, &a, &b) && (m||a||b))
{
sqrtm = sqrt(1.0*m) ;
ansp = ansq = 1 ;
for (p = 0 ; Primes[p] <= sqrtm ; p++)
{
q = min(m/Primes[p], Primes[p]*b/a) ;
while (IsPrime[q]) q-- ;
if (Primes[p]*q > ansp * ansq)
ansp = Primes[p], ansq = q ;
}
printf ("%d %d\n", ansp, ansq) ;
}
return 0 ;
}
轉(zhuǎn)載于:https://www.cnblogs.com/lzsz1212/archive/2012/01/07/2315423.html
總結(jié)
以上是生活随笔為你收集整理的[恢]hdu 1239的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: aistudio/jupyter 相关
- 下一篇: Winform跨线程调用简洁办法