您的位置:首页 > 其它

Project Euler problem 47

2012-11-06 14:04 246 查看
还是暴力

我先试着把15W个数进行素因子分解

然后4个的存起来

然后搜一下

发现还真的在15W以内

#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include <cmath>
#include <vector>
#define eps 1e-6
#define INF 1000000007
#define PI acos(-1.0)
using namespace std;
vector<int>g;
bool tag[4001];
int p[4001];
int cnt;
void get_prime() //筛出5000000内素数
{
cnt = 0;
tag[1] = 1;
for (int i = 2; i < 4000; i++)
{
if (!tag[i])
p[cnt++] = i;
for (int j = 0; j < cnt && p[j] * i < 4000; j++)
{
tag[i*p[j]] = 1;
if (i % p[j] == 0)
break;
}
}
}
void gao(int x)
{
int tx = x;
int ans = 0;
for(int i = 0; i < cnt && p[i] * p[i] <= x; i++)
{
if(x % p[i] == 0)
{
while(x % p[i] == 0) x /= p[i];
ans++;
}
}
if(x != 1) ans++;
if(ans == 4) g.push_back(tx);
}
int main()
{
get_prime();
for(int i = 10; i <= 150000; i++) gao(i);
for(int i = 0; i < g.size(); i++)
if(i + 3 < g.size() && g[i] + 1 == g[i + 1] && g[i + 1] + 1 == g[i + 2] && g[i + 2] + 1 == g[i + 3])
{
cout << g[i] << endl;
break;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: