POJ-1411 & HDOJ-1239 Calling Extraterrestrial Intelligence Again 解题报告
来源:互联网 发布:欧洲知乎 编辑:程序博客网 时间:2024/06/14 08:12
筛素数并且需要优化技巧的题,当然暴力貌似也能过,不推荐暴力。题意:很简单,给你三个正整数m,a,b(其中4 < m <= 100000 且 1 <= a <= b <= 1000)。现在让你找到两个素数p和q,使得pq <= m 且 a/b <= p/q <= 1,并且要让pq的乘积尽可能大。输出p和q。
我的解题思路:如果是暴力的话筛出m最大值以内的素数然后开始从小到大枚举p的值就是了,很明显q必定大于等于p。我初步优化的话通过a/b <= p/q这个不等式可以推出q <= pb/a,这样又限制q的值,大于pb/a的值就不用枚举了。暴力在POJ上教的600多ms,初步优化后200多ms,提交榜上还有0ms,不行了,实在想不出还要怎么优化了。
我的解题代码:
#include <stdio.h>#include <cstdlib>#include <cstring>#include <cctype>#include <cmath>#include <algorithm>using namespace std;typedef long long Long;const int N = 100001;const double eps = 1e-8;bool isprime[N];int primes[N], pn;int m, a, b;void InitRead();void DataProcess();void FastSieve(int maxn);int main(){ InitRead(); while (~scanf("%d %d %d", &m, &a, &b)) { if (a == b && b == m && m == 0) break; DataProcess(); } return 0;}void InitRead(){ memset(isprime, true, sizeof(isprime)); isprime[0] = isprime[1] = false; pn = 0; FastSieve(N-1); return;}void DataProcess(){ Long ansp, ansq, anspq = -1; double temp = (double)a / b; for (int i=0; i<pn; ++i) //枚举p { for (int j=i; j<pn; ++j) //枚举q { if ((Long)primes[i] * primes[j] > m) break; if ((Long)a * primes[j] > (Long)b * primes[i]) break; //初步优化添加的代码 if ((Long)primes[i] * primes[j] > anspq && (double)primes[i] / primes[j] >= temp - eps) { ansp = primes[i]; ansq = primes[j]; anspq = (Long)primes[i] * primes[j]; } } } printf("%lld %lld\n", ansp, ansq); return;}void FastSieve(int maxn){ for (int i=2; i<=maxn; ++i) { if (isprime[i]) primes[pn++] = i; for (int j=0; j<pn; ++j) { if (i * primes[j] > maxn) break; isprime[i * primes[j]] = false; if (i % primes[j] == 0) break; } } return;}
0 0
- POJ-1411 & HDOJ-1239 Calling Extraterrestrial Intelligence Again 解题报告
- HDOJ 1239:Calling Extraterrestrial Intelligence Again 解题报告
- POJ 1411 Calling Extraterrestrial Intelligence Again 解题报告
- HDOJ 1239 Calling Extraterrestrial Intelligence Again 13.04.21周赛结题报告
- hdoj 1239 Calling Extraterrestrial Intelligence Again
- HDOJ 1239 Calling Extraterrestrial Intelligence Again
- poj 1411 Calling Extraterrestrial Intelligence Again
- POJ 1411 Calling Extraterrestrial Intelligence Again
- POJ 1411 Calling Extraterrestrial Intelligence Again 笔记
- 【贪心】Calling Extraterrestrial Intelligence Again POJ 1411
- 1411 Calling Extraterrestrial Intelligence Again
- hdoj 1239 Calling Extraterrestrial Intelligence Again(数学)
- poj 1411 Calling Extraterrestrial Intelligence Again 筛素数
- Calling Extraterrestrial Intelligence Again
- Calling Extraterrestrial Intelligence Again
- Calling Extraterrestrial Intelligence Again
- 杭电1239 Calling Extraterrestrial Intelligence Again
- hdu 1239Calling Extraterrestrial Intelligence Again
- 产品经理分析产品的思路之nABC法
- Jackknife 刀切法
- JVM系列一:JVM内存组成及分配
- MQTT协议之Mosquitto安装和使用
- 关于论坛数据库的设计(分表分库等-转)
- POJ-1411 & HDOJ-1239 Calling Extraterrestrial Intelligence Again 解题报告
- JVM系列二:GC策略&内存申请、对象衰老
- 客户端使用公钥进行加密,服务端使用密钥进行解密
- Android字符串资源及其格式化
- centOs5.10 下安装svn服务器
- eclipse提示No grammar constraints (DTD or XML schema).....两种解决方法
- KVC 与 KVO
- lua 基础语法
- CMD常用命令