UVA10006 - Carmichael Numbers(筛选构造素数表+快速幂)

UVA10006 - Carmichael Numbers(筛选构造素数表+快速幂)

题目链接

题目大意:如果有一个合数,然后它满足任意大于1小于n的整数a, 满足a^n%n = a;这样的合数叫做Carmichael Numbers。题目给你n,然你判断是不是Carmichael Numbers。

解题思路:首先用筛选法构造素数表,判断n是否是合数,然后在用快速幂求a^2-a^(n - 1)是否满足上述的式子。快速幂的时候最好用long long ,防止相乘溢出。

代码:

#include <cstdio>
#include <cstring>
#include <cmath>

const int maxn = 65000 + 5;
typedef long long ll;

int notprime[maxn];

void init () {

    for (int i = 2; i < maxn; i++)
        for (int j = 2 * i; j < maxn; j += i)
            notprime[j] = 1;
}

ll powmod(ll x, ll n, ll mod) {

    if (n == 1)
        return x;

    ll ans = powmod(x, n / 2, mod);
    ans = (ans * ans) % mod;
    if (n % 2 == 1)
        ans *= x;
    return ans % mod;
}

bool is_carmichael(int n) {

    for (int i = 2; i < n; i++) {
        if (powmod(i, n, n) != i)
        return false;
    }
    return true;
}

int main () {

    init();
    int n;
    while (scanf ("%d", &n) && n) {

        if (notprime[n] == 0)
            printf ("%d is normal.\n", n);
        else {
            bool flag = is_carmichael(n);
            if (flag)
                printf ("The number %d is a Carmichael number.\n", n);
            else
                printf ("%d is normal.\n", n);
        }
    }
    return 0;
}
时间: 2024-08-01 14:10:24

UVA10006 - Carmichael Numbers(筛选构造素数表+快速幂)的相关文章

UVA 11582 Colossal Fibonacci Numbers!(打表+快速幂)

Colossal Fibonacci Numbers! The i'th Fibonacci number f (i) is recursively defined in the following way: f (0) = 0 and f (1) = 1 f (i+2) = f (i+1) + f (i)  for every i ≥ 0 Your task is to compute some values of this sequence. Input begins with an int

POJ 3641 素数打表+快速幂 简单题

给出2个数,p和a,2<p<=1e9,1<a<p 若p满足下面2个条件,输出yes,否则输出no 1.p不是素数 2.有a^p=a(mod p) 先判断第一个条件: 本来想用一个数组is_prime[i]表示i是不是素数的,明显,这里p太大,数组开不下 若p不是素数的话, 则p必有p=b*c,其中b<=c, 则(sqrt(p))^2=b*c,则b<=sqrt(p)<=10^4.5<10^5 所以若在10^5内存在数b满足b<p&&p%b

GCD + 素数+快速幂

1.欧几里得算法 求解最大公约数,时间复杂度在O(log max(a,b))以内,可以看出,辗转相除法是非常高效的 int gcd(int a,int b) { return (b==0)?a:gcd(b,a%b); } 2.扩展欧几里得算法 求解方程a*x+b*y=gcd(a,b),a.b.x.y均为整数,时间复杂度和辗转相除法是相同的,函数返回gcd(a,b). int gcd(int a,int b) { return (b==0)?a:gcd(b,a%b); } int extgcd(i

【构造共轭函数+矩阵快速幂】HDU 4565 So Easy! (2013 长沙赛区邀请赛)

链接 :click here~~ 题意: A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate Sn. You, a top coder, say: So easy! [解题思路] 给一张神图,推理写的灰常明白了,关键是构造共轭函数,这一点实在是要有数学知识的理论基础,推出了递推式,接下

hdu 1005 根据递推公式构造矩阵 矩阵快速幂

f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Sample Input1 1 3 //a b n1 2 100 0 0 Sample Output25 矩阵A  * 矩阵B  = 矩阵C a b        f(n-1)     f(n) 1 0        f(n-2)    f(n-1) 1 # include <iostream> 2 # include <cstdio> 3 # inclu

POJ3233 构造子矩阵+矩阵快速幂

题意:给你矩阵A,求S=A+A^1+A^2+...+A^n sol:直接把每一项解出来显然是不行的,也没必要. 我们可以YY一个矩阵: 其中1表示单位矩阵 然后容易得到: 可以看出这个分块矩阵的左下角那块就可以得到要求的解S 我们取这一块,再减去一个单位矩阵1即可. 为了保持右下角一直是1,所以右上的位置必须是0,由于需要不断移位,所以1是必要的,A是必要的,所以第一列保证移位, 第二列保证保留1,因此我们能成功构造出.... 这个题还可以根据等比矩阵的性质来进行求解...后面补(x

hdu 2256 Problem of Precision 构造整数 + 矩阵快速幂

http://acm.hdu.edu.cn/showproblem.php?pid=2256 题意:给定 n    求解   ? 思路: , 令  , 那么 , 得: 得转移矩阵: 但是上面求出来的并不是结果,并不是整数.需要加上, 由于 所以结果为 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<bits/stdc++.h> using namespace std; #define rep0

poj3070 单位矩阵(转移矩阵构造)+矩阵快速幂

太妙了..通过矩阵乘法来加速递推 #include<iostream> #include<cstring> #include<cstdio> using namespace std; #define mod 10000 int n; void mul(int f[],int a[2][2]){//一维数组和矩阵相乘 int c[2]={}; for(int i=0;i<2;i++) for(int j=0;j<2;j++) c[j]=(c[j]+(long

小规模素数表的构造方法及相关

一.判断素数 可以写一个判断素数的谓词函数,即从2开始枚举到sqrt(x)(包括).但这里参数x不能过大,过大就会因为i*i乘积过大溢出. Code: 后面的内容都是基于这个函数. 二.构造素数表 //构造素数表 int cnt=0; int prime[n+1]; for(int i=2;i<=n;++i) if(is_prime(i)) prime[cnt++]=i; 这样的话,prime 数组存储的是从 2 开始的素数序列. 三.预处理判断素数 int isp[n]; for(int i=