第十八章 数论

问题链接

ALDS1_1_C:Prime Numbers

判断n是否是质数

#include<iostream>   
#include<cstdio>  
using namespace std;

int isPrime(int x) {
    if (x < 2)
        return 0;
    else if (x == 2)
        return 1;
    if (x % 2 == 0)
        return 0;
    for (int i = 3; i * i <= x; i += 2)
        if (x % i == 0)
            return 0;
    return 1;
}
int main()
{
    int n, t, cnt = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &t);
        if (isPrime(t))
            cnt++;
    }
    printf("%d\n", cnt);
    return 0;
}

题目链接

ALDS1_1_B:Greatest Common Divisor

求两个数的最大公约数

#include<iostream>   
#include<cstdio>  
using namespace std;

int gcd(int x, int y) {
    return y ? gcd(y, x % y): x;
}
int main()
{
    int x, y;
    scanf("%d %d", &x, &y);
    printf("%d\n", gcd(x, y));
    return 0;
}

问题链接

NTL_1_B:Power

mn 的值求模1000000007

#include<iostream>   
#include<cstdio>  
using namespace std;
typedef long long LL;

LL mod_pow(LL n, LL m, LL mod) {
    LL res = 1;
    while (m > 0) {
        if (m & 1)
            res = res * n % mod;
        n = n * n % mod;
        m >>= 1;
    }
    return res;
}
int main()
{
    LL n, m;
    LL mod = 1000000007;
    scanf("%lld %lld", &n, &m);
    printf("%d\n", mod_pow(n, m , mod));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值