Sum of Consecutive Prime Numbers--POJ 2739

1、题目类型:数论、打表。

2、解题思路:水题。

3、注意事项:优化dp[i][j]>10000,否则TLE。

4、实现方法:

  
    
#include < iostream >
#include
< map >
using namespace std;

bool prime[ 10000 ];
int cnt,d[ 10000 ],dp[ 1300 ][ 1300 ];
map
< int , int > M;

void Table()
{
int i,j;
prime[
1 ] = 1 ;
prime[
2 ] = prime[ 3 ] = 0 ;
for (i = 2 ;i < 100 ;i ++ )
{
for (j = 2 ;j * i < 10000 ;j ++ )
{
prime[j
* i] = true ;
}
}
}

void Set()
{
cnt
= 1 ;
for ( int i = 1 ;i < 10000 ;i ++ )
{
if ( ! prime[i])
d[cnt
++ ] = i;
}
}

void Solve()
{
int i,j;

for (i = 1 ;i < cnt;i ++ )
{
for (j = i;j < cnt;j ++ )
{
dp[i][j]
= dp[i][j - 1 ] + d[j];
if (dp[i][j] > 10000 )
break ;
M[dp[i][j]]
++ ;
}
}
}

int main()
{
Table();
Set();
Solve();
int n;
while (cin >> n && n)
cout
<< M[n] << endl;

}

 

你可能感兴趣的:(number)