CodeForces 735D - Taxes

题意 给定一个数字 n ,要求使得找到一个最小的k,满足 n=ki=1pi ,其中 pi 均为素数。
思路 本来以为这道题目是利用素数的离散性,用类似贪心的思想,每次找到距离 n 最大的素数ppre,这样能保证 n <script type="math/tex" id="MathJax-Element-353">n</script>减小的最快。但是实际上这样这样依旧不是最好的。神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");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值