题意 给定一个数字
n
,要求使得找到一个最小的
思路 本来以为这道题目是利用素数的离散性,用类似贪心的思想,每次找到距离
n
最大的素数
#include <cstdio>
#include <string>
#include<iostream>
#include<vector>
#include <stack>
#include <queue>
#include <map>
#include <cstdlib>
#include<string.h>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<int, int>pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef vector<vector<ll> >vvi;
typedef vector<ll> vi;
const int S = 8; //随机算法判定次数,一般8~10就够了
long long pow_mod(long long a, long long n, long long mod)
{
long long ret = 1;
long long temp = a%mod;
while (n)
{
if (n & 1)ret = ret*temp%mod;
temp = temp*temp%mod;
n >>= 1;
}
return ret;
}
bool check(long long a, long long n, long long x, long long t)
{
long long ret = pow_mod(a, x, n);
long long last = ret;
for (int i = 1; i <= t; i++)
{
ret = ret*ret%n;
if (ret == 1 && last != 1 && last != n - 1)return true;//合数
last = ret;
}
if (ret != 1)return true;
else return false;
}
bool Miller_Rabin(long long n) {
if (n < 2)return false;
if (n == 2)return true;
if ((n & 1) == 0)return false;//偶数
long long x = n - 1;
long long t = 0;
while ((x & 1) == 0)
{
x >>= 1; t++;
}
//srand(time(NULL));/* *************** */
for (int i = 0; i < S; i++)
{
long long a = rand() % (n - 1) + 1;
if (check(a, n, x, t))
return false;
}
return true;
}
ll getPrePrime(ll n)
{
while (n)
{
if (Miller_Rabin(n))return n;
n--;
}
return 0;
}
int main()
{
srand(time(NULL));
ll n;
cin >> n;
if (Miller_Rabin(n))printf("1");
else if ((n & 1) == 0)printf("2");
else if (Miller_Rabin(n - 2))printf("2");
else printf("3");
//printf("%d\n", ans);
//system("pause");
}