您的位置:首页 > 其它

CodeForces 735D - Taxes

2016-12-12 21:08 316 查看
题意 给定一个数字n,要求使得找到一个最小的k,满足n=∑ki=1pi,其中pi均为素数。

思路 本来以为这道题目是利用素数的离散性,用类似贪心的思想,每次找到距离n最大的素数ppre,这样能保证n减小的最快。但是实际上这样这样依旧不是最好的。神TM的歌德巴赫猜想。。猜想直接拿来用还叫猜想吗。。不过这个猜想在一定的数据范围内是成立的吧

#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");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces