poj-2739 Sum of Consecutive Prime Numbers

呂振麒 bio photo By 呂振麒 Comment

傳送門:

Sum of Consecutive Prime Numbers

題意:

給數字,求被連續的質數加總而成的方法數。

思路:

做出質數表後爬行即可。

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void pre(){
  build();      // 造個質數表出來
  for(int i=1;i<prim.size();i++)
    prim[i] += prim[i-1];
}
void init(){
  ans = 0;
}
void sol(){
  int f1 = 0, f2 = 0;
  while(f2 < prim.size()){
    while(f1+1<f2 && prim[f2]-prim[f1]>N){
      ++f1;
    }
    if(prim[f2]-prim[f1] == N)
      ++ans;
    ++f2;
  }
  cout << ans << endl;
}
int main(){
  pre();
  while( cin >> N && N ){
    init();
    sol();
  }
}
comments powered by Disqus