【POJ3006】Dirichlet's Theorem on Arithmetic Progressions(素數篩法)

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   for   div   sp   

簡單的暴力篩法就可。

 1 #include <iostream> 2 #include <cstring> 3 #include <cmath> 4 #include <cctype> 5 #include <cstdio> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 using namespace std;10 11 const int N = 1000005, M = 500005;12 bool is[N]; int prm[M];13 int getprm (int n) {14     int i, j, k = 0;15     int s, e = (int)(sqrt (0.0 + n) + 1);16     memset (is, 1, sizeof(is));17     prm[k ++] = 2; is[0] = is[1] = 0;18     for (i = 4; i < n; i += 2) is[i] = 0;19     for (i = 3; i < e; i += 2) {20         if (is[i]) {21             prm[k ++] = i;22             for (s = i * 2, j = i * i; j < n; j += s) {23                 is[j] = 0;24             }25         }26     }27     for (; i < n; i += 2) {28         if (is[i]) prm[k ++] = i;29     }30     return k;31 }32 33 int main () {34     ios :: sync_with_stdio(false);35     //freopen("out.txt", "w", stdout);36     getprm(1000000);37     /*for (int i = 0 ; i < 78499; ++ i) {38         cout << i  << " : " << prm[i] << endl;39     }*/40     long long a, d, n;41     long long cnt = 0, cur = 0;42     while (cin >> a >> d >> n) {43         if (a == 0 && d == 0 && n == 0) break;44         cnt = 0 ;45         while (1) {46             if (binary_search(prm, prm + 78499, a)) {47                 cnt ++;48                 if (cnt == n) break;49             }50             a += d;51             //cout << "step: " << cnt << " " << a << endl;52         }53         cout << a << endl;54     }55     return 0;56 }

 

【POJ3006】Dirichlet's Theorem on Arithmetic Progressions(素數篩法)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.