uva 10006 Carmichael

比较基本的数论题目,不过《挑战程序设计》上说这个题有不用幂运算求解的两种方法,一种复杂度为根号n,一种是O(n)预处理,O(1)判定,我还没有想出来....

快速幂的方法:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cmath>
 4 using namespace std;
 5
 6 typedef long long ll;
 7 const int N = 65001;
 8 bool visit[N];
 9
10 void sieve( int n )
11 {
12     int r = sqrt( n + 0.5 );
13     memset( visit, 0, sizeof(visit) );
14     visit[0] = visit[1] = 1;
15     for ( int i = 2; i <= r; i++ )
16     {
17         if ( !visit[i] )
18         {
19             for ( int j = i * i; j <= n; j += i )
20             {
21                 visit[j] = 1;
22             }
23         }
24     }
25 }
26
27 ll pow_mod( ll a, ll n, ll mod )
28 {
29     ll ans = 1, w = a % mod;
30     while ( n )
31     {
32         if ( n & 1 )
33         {
34             ans = ans * w % mod;
35         }
36         w = w * w % mod;
37         n >>= 1;
38     }
39     return ans;
40 }
41
42 int main ()
43 {
44     sieve( N - 1 );
45     int n;
46     while ( cin >> n, n )
47     {
48         bool flag = true;
49         if ( visit[n] )
50         {
51             for ( int x = 2; x < n; x++ )
52             {
53                 int tmp = pow_mod( x, n, n );
54                 if ( tmp != x )
55                 {
56                     flag = false;
57                     break;
58                 }
59             }
60         }
61         else
62         {
63             flag = false;
64         }
65         if ( flag )
66         {
67             cout << "The number " << n << " is a Carmichael number." << endl;
68         }
69         else
70         {
71             cout << n << " is normal." << endl;
72         }
73     }
74     return 0;
75 }
时间: 2024-08-01 19:52:05

uva 10006 Carmichael的相关文章

UVA - 10006 - Carmichael Numbers (快速幂+素数判断)

题目传送:UVA - 10006 思路:就是快速幂暴力过去就行了,然后要注意点细节,就是快速幂的时候会爆int,然后就是先判断是否为素数,是素数就直接输出结果is normal,不然会超时 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #inclu

uva 10006 Carmichael Numbers

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=947 打出素数表,快速幂取模. 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #define ll long long 6

UVa 10006 Carmichael Numbers (快速幂 + 素性测试)

Carmichael Numbers Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Carmichael Numbers  An important topic nowadays in computer science is cryptography. Some people even think that cryptography is t

Carmichael Numbers(Uva 10006)

  Carmichael Numbers An important topic nowadays in computer science is cryptography. Some people even think that cryptography is the only important field in computer science, and that life would not matter at all without cryptography. Alvaro is one

uva 10006 数论入门题

这是一个入门的数论题目 , 只需要简单的找素数和快速幂取模 题意:输入一个数 n , 如果这个数是非素数 , 问是不是 这个2~n-1区间的所有数都满足 ? 解法:由于数据量不大 , 可以直接暴力求解 解法1: 暴力求解 #include <iostream> #include <string.h> #include <stdio.h> using namespace std; long long prime[65010]; long long n; void init

UVa 10006 快速幂运算

知识补充: 如果a和b关于m同于,那么(a - b )是m的整数倍. 负数取余运算:不同的语言有不同的方法,其中C和JAVA是按第一个数的符号确定结果的符号,C++则要依据系统而定. 取余运算的发展: 1.(a+b)%p=(a%p+b%p)%p 2.(a?b)%p=(a%p?b%p)%p 3.(a?b)%p=(a%p?b%p)%p 4.(ab)%p=((a%p)b)%p 费马定理:对于一个不能被p整除的数a: ap≡a mod p ap?1≡1 mod p 快速幂算法(求xn%mod): 有两种

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

计划,,留

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 一.<算法竞赛入门经典> 刘汝佳 (UVaOJ 351道题) 以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html "AOAPC I"

算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发.   一.UVaOJ http://uva.onlinejudge.org  西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ.   二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html   "AO