hdu 2098 分拆素數和

來源:互聯網
上載者:User

標籤:

題目串連

http://acm.hdu.edu.cn/showproblem.php?pid=2098

分拆素數和Description

把一個偶數拆成兩個不同素數的和,有幾種拆法呢?

Input

輸入包含一些正的偶數,其值不會超過10000,個數不會超過500,若遇0,則結束。

Output

對應每個偶數,輸出其拆成不同素數的個數,每個結果佔一行。

SampleInput

30

26

0

SampleOutput

3

2

打表,二分。。
 1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<map> 8 #include<set> 9 using std::cin;10 using std::cout;11 using std::endl;12 using std::find;13 using std::sort;14 using std::set;15 using std::map;16 using std::pair;17 using std::vector;18 using std::lower_bound;19 #define sz(c) (int)(c).size()20 #define all(c) (c).begin(), (c).end()21 #define iter(c) __typeof((c).begin())22 #define cls(arr,val) memset(arr,val,sizeof(arr))23 #define cpresent(c, e) (find(all(c), (e)) != (c).end())24 #define rep(i, n) for (int i = 0; i < (int)(n); i++)25 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)26 #define pb(e) push_back(e)27 #define mp(a, b) make_pair(a, b)28 const int Max_N = 10010;29 typedef unsigned long long ull;30 struct PrimeSpilt {31     int tot, prime[Max_N];32     inline bool isPrime(int n) {33         for(int i = 2; i * i <= n; i++) {34             if(!(n % i)) return false;35         }36         return n != 1;37     }38     inline void init() {39         tot = 0;40         for(int i = 1;i < Max_N ;i++) {41             if(isPrime(i)) prime[tot++] = i;42         }43     }44     inline void work(int n) {45         int ans = 0;46         for(int i = 0;i < tot && prime[i] < n; i++) {47             int p = lower_bound(prime + i, prime + tot, n - prime[i]) - prime;48             if(prime[i] + prime[p] == n && i != p) ans++;49         }50         printf("%d\n", ans);51     }52 }go;53 int main() {54 #ifdef LOCAL55     freopen("in.txt","r",stdin);56     freopen("out.txt","w+",stdout);57 #endif58     int n;59     go.init();60     while(~scanf("%d", &n) && n) go.work(n);61     return 0;62 }
View Code

 

hdu 2098 分拆素數和

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.