我堅持從Codechef實踐問題難度介質的問題,有問題的聲明:Optimizating素數序列
Shridhar要產生自己的密碼一些素數。 幫助他!你的任務是兩個給定 數字
Rest of the description, I/O format, Test cases examples on this question link
我的執行是得到一個TLE(時間超出限制)的問題,並希望解決這個問題之間產生的所有素數,你可以指出任何在我的實施中出現了問題,我似乎無法在空轉幾個小時後弄清楚。
包括指令和ifPrime功能
#include<map>
#include<math.h>
#include<stdio.h>
#define LLD long long int
#define REPNE(i,a,b) for(LLD i=a; i<=b; ++i)
#define REPNEI(i,a,b,k) for(LLD i=a; i<=b; i+=k)
using namespace std;
map<LLD, bool> mem;
bool ifPrime (LLD a) {
if (a<2) return false;
else if (a==2) return true;
else if (a%2==0) return false;
REPNEI(i,3,sqrt(a),2) {
if (a%i==0) return false;
}
mem[a] = true;
return true;
}
生成主要功能
void genPrime (LLD a, LLD b) {
REPNE(i,a,b) {
if (mem.find(i) != mem.end()) printf("%lld\n", i);
else if (ifPrime(i)) printf("%lld\n", i);
} printf("\n");
}
主要
int main () {
// freopen("input.txt", "r", stdin);
int t; scanf("%d", &t);
REPNE(test, 1, t) {
LLD a, b;
scanf("%lld %lld", &a, &b);
genPrime(a,b);
}
}
我想不出其他的解決這個問題,唯一的辦法我想到了memoization,它也處理大整數。需要幫助。
你真的認爲'REPNE'比簡單循環更具可讀性嗎? – Jarod42
您可以使用[Sieve_of_Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)。 – Jarod42
參與競爭性編程的人發現宏比編寫完整循環更容易,因爲速度在競爭中確實很重要,因爲在實際的現場開發中,應該避免這些以確保團隊成員之間的可讀性@ Jarod42 –