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;
}

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

  1. 埃氏筛法求素数&构造素数表求素数

    埃氏筛法求素数和构造素数表求素数是一个道理. 首先,列出从2开始的所有自然数,构造一个序列: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1 ...

  2. UVA10006 - Carmichael Numbers

    题目链接:UVA10006 本来想直接打素数表,然后根据素数表来判断,结果一直超时,后来把素数表去掉,再在for循环中加判断才勉强过了. Some numbers that are not prime ...

  3. 【UVA - 10006 】Carmichael Numbers (快速幂+素数筛法)

    -->Carmichael Numbers  Descriptions: 题目很长,基本没用,大致题意如下 给定一个数n,n是合数且对于任意的1 < a < n都有a的n次方模n等于 ...

  4. UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)

      Carmichael Numbers  An important topic nowadays in computer science is cryptography. Some people e ...

  5. poj 2262【素数表的应用---判断素数】【哈希】

    Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35214   Accepted: ...

  6. Carmichael Numbers - PC110702

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10006.html 原创:Carm ...

  7. <编程>比较两种素数表生成算法+计算程序运行时间+通过CMD重定向测试程序

    最近学习加密算法,需要生成素数表,一开始使用简单的循环,从2开始判断.代码如下: #include<iostream> #include<cstdio> #include< ...

  8. UVa 10006 - Carmichael Numbers

    UVa 10006 - Carmichael Numbers An important topic nowadays in computer science is cryptography. Some ...

  9. sql 游标例子 根据一表的数据去筛选另一表的数据

    sql 游标例子 根据一表的数据去筛选另一表的数据 DECLARE @MID nvarchar(20)DECLARE @UTime datetime DECLARE @TBL_Temp table( ...

随机推荐

  1. 【BUG】&quot;main&quot; prio=5 tid=1 RUNNABLE

    载入超大效果图导致内存不足(GC/ANR) 06-30 11:42:56.624: D/dalvikvm(16264): GC_CONCURRENT freed 1982K, 7% free 4537 ...

  2. 湖南省第八届大学生计算机程序设计竞赛(A,B,C,E,F,I,J)

    A 三家人 Description 有三户人家共拥有一座花园,每户人家的太太均需帮忙整理花园.A 太太工作了5 天,B 太太则工作了4 天,才将花园整理完毕.C 太太因为正身怀六甲无法加入她们的行列, ...

  3. HDU 4704 Sum 超大数幂取模

    很容易得出答案就是2^(n-1) 但是N暴大,所以不可以直接用幂取模,因为除法操作至少O(len)了,总时间会达到O(len*log(N)) 显然爆的一塌糊涂 套用FZU1759的模板+顺手写一个大数 ...

  4. jQuery对象与js对象转换

    前言 jq方法和js的方法属性是不能互相使用的,所以有时候就需要转一下,下面就介绍下方法. js对象转化为jQuery对象 var box=document.getElementById(" ...

  5. col---过滤控制字符

  6. cron 简单任务调度 go

    package main import ( "github.com/robfig/cron" "log" ) func main() { i := 0 c := ...

  7. poj3244(公式题)

    Difference between Triplets Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 2476   Acce ...

  8. JavaScript中的global对象,window对象以及document对象的区别和联系

    JavaScript中的global对象,window对象以及document对象的区别和联系 一.概念区分:JavaScript中的global对象,window对象以及document对象 1.g ...

  9. javascript创建对象的方法--动态原型模式

    javascript创建对象的方法--动态原型模式 一.总结 1.作用:解决组合模式的属性和函数分离问题  2.思路:基本思路和组合模式相同:共用的函数和属性用原型方式,非共用的的函数和属性用构造函数 ...

  10. golang pipe

    ===============golang pipe============== package main import ( "fmt" "io" ) func ...