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 #define maxn 65000
 7 using namespace std;
 8
 9 int n;
10 bool vis[maxn*10];
11
12
13 ll pow_mod(ll a,ll p,ll n)
14 {
15     if(p==0) return 1;
16     ll ans=pow_mod(a,p/2,n);
17     ans=ans*ans%n;
18     if(p%2==1) ans=ans*a%n;
19     return ans;
20 }
21 void Get_prime()
22 {
23     memset(vis,false,sizeof(vis));
24     int m=(int)sqrt(maxn*1.0);
25     for(int i=2; i<=m; i++)
26     {
27         if(!vis[i])
28         {
29             for(int j=i*i; j<=maxn; j+=i)
30             {
31                 vis[j]=true;
32             }
33         }
34     }
35 }
36
37
38 int main()
39 {
40     Get_prime();
41     while(scanf("%d",&n)!=EOF)
42     {
43         if(n==0) break;
44         if(!vis[n])
45         {
46             printf("%d is normal.\n",n);
47             continue;
48         }
49         bool flag=true;
50         for(int i=2; i<n; i++)
51         {
52             ll c=pow_mod((ll)i,(ll)n,(ll)n);
53             if(c!=i)
54             {
55                 flag=false;
56                 break;
57             }
58         }
59         if(flag)
60         {
61             printf("The number %d is a Carmichael number.\n",n);
62         }
63         else printf("%d is normal.\n",n);
64     }
65     return 0;
66 }

时间: 2024-10-24 22:12:36

uva 10006 Carmichael Numbers的相关文章

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 (快速幂 + 素性测试)

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

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 vis

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 - 12046 Great Numbers

Description Problem G - Great Numbers In this problem you have to count the number of great numbers of length n. Here a great number must have the following property: the number must be divisible by all of its decimal digits. it does not contain any

UVA - 471 Magic Numbers

Description  Magic Numbers  Write a program that finds and displays all pairs ofintegers and such that: neither nor have any digits repeated; and , where N is a given integer; Input and Output The input file consist a integer at the beginning indicat

Uva - 12050 Palindrome Numbers【数论】

题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然后就得到是长度为多少的第几个的回文串了,有个细节注意的是, n计算完后要-1! 下面给出AC代码: 1 #include <bits/stdc++.h> 2 typedef long long ll; 3 using namespace std; 4 const int maxn=3010; 5