zoukankan      html  css  js  c++  java
  • uva 10006 Carmichael Numbers

    如果一个数n不是素数,却满足对于所有a(a>=2 && a<=n-1),a^n mod n == a,就说他是Carmichael number,否则是normal的。

    快速幂计算,范围会超int。

    代码:

    #include <stdio.h>
    #include <math.h>
    int prime(int a) {
        if(a <= 1)return 0;
        if(a == 2 || a == 3)return 1;
        if(a % 6 != 1 && a % 6 != 5)return 0;
        for(int i = 5;i * i <= a;i += 6) {
            if(a % i == 0 || a % (i + 2) == 0)return 0;
        }
        return 1;
    }
    long long pow_n(long long a,long long b) {
        long long d = 1,c = b;
        while(b) {
            if(b % 2)d = (d * a) % c;
            a = (a * a) % c;
            b /= 2;
        }
        return d;
    }
    int check(int a) {
        for(int i = 2;i < a;i ++) {
            if(pow_n(i,a) != i)return 0;
        }
        return 1;
    }
    int main() {
        int a;
        while(~scanf("%d",&a) && a) {
            if(!prime(a) && check(a))printf("The number %d is a Carmichael number.
    ",a);
            else printf("%d is normal.
    ",a);
        }
    }
  • 相关阅读:
    camp训练day2
    LCA板子题
    牛客多校第一场
    P1063 能量项链 区间DP
    64. Minimum Path Sum
    46. Permutations
    216. Combination Sum III
    62. Unique Paths
    53. Maximum Subarray
    22. Generate Parentheses
  • 原文地址:https://www.cnblogs.com/8023spz/p/9347219.html
Copyright © 2011-2022 走看看