数论初步复习(hdu5391,hdu2973,HihoCoder 1287,hdu2138)

威尔逊定理

hdu5391
题目大意:给出T个数,每个数为a,输出(a-1)!moda
应用威尔逊定理,如果是素数的话,((a-1)!+1) mod a == 0或者表示为(a − 1)! ≡ −1 (mod a) ≡是取模的意思

//
//  main.cpp
//  威尔逊定理_素数问题
//
//  Created by 陈冉飞 on 2019/8/18.
//  Copyright © 2019 陈冉飞. All rights reserved.
//

#include <iostream>
using namespace std;
int T,a;

bool is_prime(int x){
    for (int i = 2; i*i < x; i++) {
        if (x%i == 0) return false;
    }
    return true;
}

int main(int argc, const char * argv[]) {
    for (scanf("%d",&T); T; T--) {
        scanf("%d",&a);
        if (a == 4) printf("2\n");
        else if (is_prime(a)) printf("%d\n",a-1);
        else printf("0\n");
    }
    return 0;
}

威尔逊定理

hdu2973
这个题看懂了之后就是看对于k,如果3k+7是素数(因为看到3k+6和3k+7很容易想到威尔逊定理,加一可整除),则求和(前缀和)加上1,然后如果不是了就不加一。
注意点:就是在每次的判断是否是素数是,由于数太大,每次都判断一次就会超时,所以提前打表,init一下把素数标记为零。

//
//  main.cpp
//  威尔逊定理_素数_hdu2973
//
//  Created by 陈冉飞 on 2019/8/18.
//  Copyright © 2019 陈冉飞. All rights reserved.
//

#include <iostream>
using namespace std;
#define maxn 1001000
int T,a,sum[maxn*3],is_prime[maxn*3];

void init(){
    for(int i = 2; i < maxn*3; i++)
        if(is_prime[i]==0)
            for(int j=i+i;j < maxn*3;j+=i)
                is_prime[j]=1;
}

int getb(int x){
    if (!is_prime[3*x+7]) return 1;
    else return 0;
}

void getsum(){
    sum[0] = 0;sum[1] = 0;
    for (int i = 2; i <maxn; i++) sum[i] = sum[i-1]+getb(i);
}

int main(int argc, const char * argv[]) {
    init();
    getsum();
    for (scanf("%d",&T); T; T--) {
        scanf("%d",&a);
        printf("%d\n",sum[a]);
    }
    
    return 0;
}

素性判断

素性检验以及素性检验复杂度只指的是当检验一个数是否为素数的时间复杂的,别的暂不讨论


朴素法:

O(n)

for(int i = 2;i < x;i++){
	if(x%i == 0)return false; 
}
return true;

O(sqrt(n))

也叫试除法

for(int i = 2;i*i < x;i++){
	if(x%i == 0)return false;
}
return true;

Miller_Rabin素性测试

补充知识,
费马小定理:如果已知了某个数n为奇素数,然后任意小于他的数a(a<n)满足a^(n-1)≡1(mod n),也就是(a^(n-1)+1)mod n = 0,也就是a和n-1的异或加一可以整除n
要测试n是否是一个素数,首先将n-1分解为(2^s) * d,在每次测试开始时,先随机选择一个介于[1, N - 1]的整数a,之后如果对于所有的r∈[0, s - 1],若a^d mod N ≠ 1且a ^ ((2 ^ r) * d) mod N ≠ -1,那么n就是一个合数,否则n有3/4的几率是素数。

模版:

typedef long long LL;

LL mul_mod(LL a, LL b, LL mod)
{
    LL ans = 0;
    while(b)
    {
        if(b&1) ans = (ans + a) % mod;
        a = (a + a) % mod;
        b >>= 1;
    }
    return ans;
}

LL pow_mod(LL a, LL n, LL mod)
{
    LL ans = 1;
    while(n)
    {
        if(n & 1) ans = mul_mod(ans, a, mod);
        a = mul_mod(a, a, mod);
        n >>= 1;
    }
    return ans;
}

bool Miller_Rabin(LL n) {
    LL u = n - 1, pre, x;
    int i, j, k = 0;
    if(n == 2 || n == 3 || n == 5 || n == 7 || n == 11) return true;
    if(n == 1 || (!(n % 2)) || (!(n % 3)) || (!(n % 5)) || (!(n % 7)) || (!(n % 11))) return
        false;
    for(; !(u & 1); k++, u >>= 1);
    for(i = 0; i < 5; i++) {
        x = rand() % (n - 2) + 2;
        x = pow_mod(x, u, n);
        pre = x;
        for(j = 0; j < k; j++) {
            x = mul_mod(x, x, n);
            if(x == 1 && pre != 1 && pre != (n - 1))
                return false;
            pre = x;
        }
        if(x != 1) return false;
    }
    return true;
}

HihoCoder 1287
裸的利用miller-rabin素性测试来测定是否为质数

//
//  main.cpp
//  Miller-Rabin质数测试
//
//  Created by 陈冉飞 on 2019/8/18.
//  Copyright © 2019 陈冉飞. All rights reserved.
//

#include <iostream>
using namespace std;
typedef long long LL;
LL T,tem;

LL mul_mod(LL a, LL b, LL mod)
{
    LL ans = 0;
    while(b)
    {
        if(b&1) ans = (ans + a) % mod;
        a = (a + a) % mod;
        b >>= 1;
    }
    return ans;
}

LL pow_mod(LL a, LL n, LL mod)
{
    LL ans = 1;
    while(n)
    {
        if(n & 1) ans = mul_mod(ans, a, mod);
        a = mul_mod(a, a, mod);
        n >>= 1;
    }
    return ans;
}

bool Miller_Rabin(LL n) {
    LL u = n - 1, pre, x;
    int i, j, k = 0;
    if(n == 2 || n == 3 || n == 5 || n == 7 || n == 11) return true;
    if(n == 1 || (!(n % 2)) || (!(n % 3)) || (!(n % 5)) || (!(n % 7)) || (!(n % 11))) return
        false;
    for(; !(u & 1); k++, u >>= 1);
    for(i = 0; i < 5; i++) {
        x = rand() % (n - 2) + 2;
        x = pow_mod(x, u, n);
        pre = x;
        for(j = 0; j < k; j++) {
            x = mul_mod(x, x, n);
            if(x == 1 && pre != 1 && pre != (n - 1))
                return false;
            pre = x;
        }
        if(x != 1) return false;
    }
    return true;
}

int main(int argc, const char * argv[]) {
    for (scanf("%lld",&T); T; T--) {
        scanf("%lld",&tem);
        if (Miller_Rabin(tem)) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}

hdu2138
miller-rabin的应用:判断输入的这些数中的素数的个数,接着套板子

//
//  main.cpp
//  Miller-Rabin应用_hdu2138
//
//  Created by 陈冉飞 on 2019/8/18.
//  Copyright © 2019 陈冉飞. All rights reserved.
//

#include <iostream>
using namespace std;
typedef long long LL;
LL n,tem;

LL mul_mod(LL a, LL b, LL mod)
{
    LL ans = 0;
    while(b)
    {
        if(b&1) ans = (ans + a) % mod;
        a = (a + a) % mod;
        b >>= 1;
    }
    return ans;
}

LL pow_mod(LL a, LL n, LL mod)
{
    LL ans = 1;
    while(n)
    {
        if(n & 1) ans = mul_mod(ans, a, mod);
        a = mul_mod(a, a, mod);
        n >>= 1;
    }
    return ans;
}

bool Miller_Rabin(LL n) {
    LL u = n - 1, pre, x;
    int i, j, k = 0;
    if(n == 2 || n == 3 || n == 5 || n == 7 || n == 11) return true;
    if(n == 1 || (!(n % 2)) || (!(n % 3)) || (!(n % 5)) || (!(n % 7)) || (!(n % 11))) return
        false;
    for(; !(u & 1); k++, u >>= 1);
    for(i = 0; i < 5; i++) {
        x = rand() % (n - 2) + 2;
        x = pow_mod(x, u, n);
        pre = x;
        for(j = 0; j < k; j++) {
            x = mul_mod(x, x, n);
            if(x == 1 && pre != 1 && pre != (n - 1))
                return false;
            pre = x;
        }
        if(x != 1) return false;
    }
    return true;
}

int main(int argc, const char * argv[]) {
    while (~scanf("%lld",&n)) {
        LL ans = 0;
        while (n--) {
            scanf("%d",&tem);
            ans += Miller_Rabin(tem);
        }
        printf("%lld\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值