数论

  Carmichael Numbers

(Uva 10006)

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 of such persons, and is designing a set of cryptographic procedures for cooking paella. Some of the cryptographic algorithms he is implementing make use of big prime numbers. However, checking if a big number is prime is not so easy. An exhaustive approach can require the division of the number by all the prime numbers smaller or equal than its square root. For big numbers, the amount of time and storage needed for such operations would certainly ruin the paella.

However, some probabilistic tests exist that offer high confidence at low cost. One of them is the Fermat test.

Let a be a random number between 2 and n - 1 (being n the number whose primality we are testing). Then, n is probably prime if the following equation holds:

egin{displaymath}a^n mod n = a
end{displaymath}


If a number passes the Fermat test several times then it is prime with a high probability.

Unfortunately, there are bad news. Some numbers that are not prime still pass the Fermat test with every number smaller than themselves. These numbers are called Carmichael numbers.

In this problem you are asked to write a program to test if a given number is a Carmichael number. Hopefully, the teams that fulfill the task will one day be able to taste a delicious portion of encrypted paella. As a side note, we need to mention that, according to Alvaro, the main advantage of encrypted paella over conventional paella is that nobody but you knows what you are eating.

Input 

The input will consist of a series of lines, each containing a small positive number n ( 2 < n < 65000). A number n = 0 will mark the end of the input, and must not be processed.

Output 

For each number in the input, you have to print if it is a Carmichael number or not, as shown in the sample output.

Sample Input 

1729
17
561
1109
431
0

Sample Output 

The number 1729 is a Carmichael number.
17 is normal.
The number 561 is a Carmichael number.
1109 is normal.
431 is normal.

 水题一道:快速幂+素数判断

[ 关于快速幂的知识 ]

快速幂是一种快速求出n^m的方法,一般求n^m时都是用:n^m=n*n*n*n*n.......n的方法,这种纯模拟的方法时间复杂度是O(m),如果数据太大的话很容易超时,所以快速幂就显得极其重要。

1.快速幂的原理:二分法

举个例子,我们在算2^4的时候,先算2^2=4,这个时候就有两种选择,一种是继续算2*2*2*2,一种是直接平方,即2^4=(2^2)*(2*2)=(2*2)^2,快速幂就是这样来优化时间复杂度的。

再例如:

a^b=a*a*a*a*a*......*a (b个a 相乘) 如果直接计算要做 b-1 次乘法。

可以利用幂运算的性质: a^b=(a^c)*(a^d)=a^(c+d)  其中b=c+d

如果把b平均分为两份,每份的数量为c=d=b/2, a^c和a^d的值一样,就可以重复利用。

这样分一次就只要做 b/2-1+1=b/2次的乘法,若b为奇数,则要做b/2+1次乘法,于是乘法的次数就大大减少,

这样重复分几次后,原式的值就能很快地算出来。

快速幂的递归实现代码:

long long quick_pow(long a,long n,long b)
{
long long t;
if(n==0) return 1%b;
if(n==1) return a%b;
t=quick_pow(a,n/2,b);
t=t*t%b;
if((n&1)==1) t=t*a%b;
return t;
}

非递归实现代码:

long long quick_pow(long long a,long long b,long long k)  //求(a^b)%k
{
    long long ans=1;
    while(b)
    {
        if(b%2)   //b是奇数
          ans=(ans*a)%k;
        b=b/2;
        a=(a*a)%k;
    }
    return ans;
}

递归虽然好理解,但是极其容易爆栈,所以一般本人不使用递归的快速幂;

即使不用递归,这儿还存在一个问题,就是刘汝佳在小白书里面反复提到的乘法溢出,当数据很大时,就很有可能乘法溢出,这儿就得用到快速幂的变式:

    快速加法: a*b=a*(b/2)+a*(b/2)

                          (a+b)%m=(a%m+b%m)%m

这样就有效的避免了乘法溢出的问题,但相应的代码就复杂了一点。

inline unsigned long long quickadd(unsigned long long a,unsigned long long m,unsigned long long p)
{
   unsigned long long ans=0;
   while (a)
   {
       if (a&1) ans=(ans+m)%p;
       a=a>>1;
       m=(m*2)%p;
   }
   return ans;
}
void quick(unsigned long long n,unsigned long long m,unsigned long long p)
{
  while (n)
  {
      if (n&1) ans=(quickadd(ans,m))%p;
      n=n>>1;
      m=(quickadd(m,m))%p;
  }
}

unsigned long long的输入输出用%I64d.

本题的AC代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
bool mark[65005];
bool Is_prime(int n)
{
    int i;
    bool ans=true;
    if(n==1)
        return false;
    if(n%2==0)
        return false;
    else
    for(i=3;i*i<=n;i=i+2)
    {
        if(n%i==0)
        {
            ans=false;
            break;
        }
    }
    return ans;
}
void Mark()
{
    int i;
    for(i=1;i<65005;i++)
    {
        if(Is_prime(i))
        {
            mark[i]=true;
        }
        else
        mark[i]=false;
    }
}
long long quick_pow(long long a,long long b,long long k)  //求(a^b)%k
{
    long long ans=1;
    while(b)
    {
        if(b%2)   //b是奇数
          ans=(ans*a)%k;
        b=b/2;
        a=(a*a)%k;
    }
    return ans;
}
int main()
{
    Mark();
    long long n;
    while(scanf("%lld",&n),n)
    {
        if(mark[n])
        {
            printf("%lld is normal.
",n);
            continue;
        }
        long long i;
        bool ans=1;
        for(i=2;i<n;i++)
        {
//            cout<<quick_pow(i,n,n)<<"  "<<i<<endl;
            if(quick_pow(i,n,n)!=i)
            {
                ans=0;
                break;
            }
        }
        if(ans)
            printf("The number %lld is a Carmichael number.
",n);
        else
            printf("%lld is normal.
",n);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/crazyacking/p/3699541.html