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;
}
UVA10006 - Carmichael Numbers(筛选构造素数表+高速幂)的更多相关文章
- 埃氏筛法求素数&构造素数表求素数
埃氏筛法求素数和构造素数表求素数是一个道理. 首先,列出从2开始的所有自然数,构造一个序列: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1 ...
- UVA10006 - Carmichael Numbers
题目链接:UVA10006 本来想直接打素数表,然后根据素数表来判断,结果一直超时,后来把素数表去掉,再在for循环中加判断才勉强过了. Some numbers that are not prime ...
- 【UVA - 10006 】Carmichael Numbers (快速幂+素数筛法)
-->Carmichael Numbers Descriptions: 题目很长,基本没用,大致题意如下 给定一个数n,n是合数且对于任意的1 < a < n都有a的n次方模n等于 ...
- UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)
Carmichael Numbers An important topic nowadays in computer science is cryptography. Some people e ...
- poj 2262【素数表的应用---判断素数】【哈希】
Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35214 Accepted: ...
- Carmichael Numbers - PC110702
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10006.html 原创:Carm ...
- <编程>比较两种素数表生成算法+计算程序运行时间+通过CMD重定向测试程序
最近学习加密算法,需要生成素数表,一开始使用简单的循环,从2开始判断.代码如下: #include<iostream> #include<cstdio> #include< ...
- UVa 10006 - Carmichael Numbers
UVa 10006 - Carmichael Numbers An important topic nowadays in computer science is cryptography. Some ...
- sql 游标例子 根据一表的数据去筛选另一表的数据
sql 游标例子 根据一表的数据去筛选另一表的数据 DECLARE @MID nvarchar(20)DECLARE @UTime datetime DECLARE @TBL_Temp table( ...
随机推荐
- julia/pyplot 绘图加入标签和标题
julia 调用matplotlib.pyplot 须要先using pycall 先安装pycall Pkg.add("PyCall") 然后吧. . . 上代码把:(应该是通俗 ...
- enable&&builtin---shell内部命令
用enable命令显示所有激活的内部命令: [root@localhost ~]# enable -a builtin命令用于执行指定的shell内部命令,并返回内部命令的返回值 [root@xiao ...
- 地道的 Python(二)
作者: Zhang Yang 列表推导 上文介绍了一个高逼格的创建字典的方法.那列表呢?依据蛋痛定律,它也一定有,可是它被起了一个很蛋痛的名字,叫列表推导: 先看看这种代码: li = [] for ...
- Pig源代码分析: 简析运行计划的生成
摘要 本文通过跟代码的方式,分析从输入一批Pig-latin到输出物理运行计划(与launcher引擎有关,通常是MR运行计划.也能够是Spark RDD的运行算子)的总体流程. 不会详细涉及AST怎 ...
- String类型转Long类型需要注意的问题
转自:https://blog.csdn.net/m819177045/article/details/52669785/
- 洛谷 P2679 子串
题目背景 无 题目描述 有两个仅包含小写英文字母的字符串 A 和 B.现在要从字符串 A 中取出 k 个互不重叠的非空子串,然后把这 k 个子串按照其在字符串 A 中出现的顺序依次连接起来得到一 个新 ...
- Vue路由query传参
1.不要进行过深的嵌套 let id = 'uyu' this.$router.push({ path: '/mrp_detail', query: { re_order_id: id, option ...
- Direct2D 图形计算
D2D不仅可以绘制,还可以对多个几何图形对象进行空间运算.这功能应该在GIS界比较吃香. 这些计算包括: 合并几何对象,可以设置求交还是求并,CombineWithGeometry 边界,加宽边界,查 ...
- Google Web Toolkit(GWT) 在windows下环境搭建
1.什么是GWT? Google Web Toolkit(简称GWT,读作/ˈɡwɪt/),是一个前端使用JavaScript,后端使用Java的AJAX framework,以Apache许可证2. ...
- Solr的关键特性
1.基于标准的开放接口:Solr搜索服务器支持通过XML.JSON和HTTP查询和获取结果. 2.易管理:Solr可以通过HTML页面管理,Solr配置通过XML完成. 3.可伸缩性:能够有效地复制到 ...