zoukankan      html  css  js  c++  java
  • UVa 10006

    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 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: a^n mod n = a
    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.

    迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……

    #include <iostream>
    using namespace std;
    int prime[65000];
    long long powmod(int a,int n,int m)
    {
        if (n==1)return a%m;
        long long x=powmod(a,n/2,m);
        x=(x*x)%m;
        if (n%2)x=(x*a)%m;
        return x;
    }
    int tests(int n)
    {
        for (int i=2; i<n; ++i)
            if (powmod(i,n,n)!=i)
                return 0;
        return 1;
    }
    int main()
    {
        for (int i =0; i<65000; ++i)
            prime[i]=1;
        for (int i=2; i<65000; ++i)
            if (prime[i])
                for (int j=2*i; j<65000; j+=i)
                    prime[j]=0;
        int n;
        while(cin>>n&&n)
            if(!prime[n]&&tests(n))
                cout<< "The number "<<n<<" is a Carmichael number."<<endl;
            else cout<<n<<" is normal."<<endl;
        return 0;
    }
    

  • 相关阅读:
    The method getDispatcherType() is undefined for the type HttpServletRequest错误解决方法
    OpenGL编程逐步深入(三)在窗口中显示一个三角形
    OpenGL编程逐步深入(二)在窗口中显示一个点
    开源3D游戏引擎Irrlicht简介
    OpenGL编程逐步深入(一)创建一个窗口
    用lua实现ByteArray和ByteArrayVarint
    lua 的io操作,非常详细
    Lua日期与时间操作
    Lua语言开发Cocos2d-x游戏视频教程第L0401课-Cocos2d-x中使用Lua
    Golang 类型转换整理
  • 原文地址:https://www.cnblogs.com/im0qianqian/p/5989538.html
Copyright © 2011-2022 走看看