zoukankan      html  css  js  c++  java
  • UVa 10006 Carmichael Numbers(快速幂)

    思路:

    1.先判断是不是素数;
    2.再按题意遍历2~n-1即可,用快速幂加速就可以了;(注意用long long

    代码:

    #include<iostream>
    using namespace std;
    typedef long long LL;
    bool isPrime(int n){
    	for(int i=2;i*i<=n;i++) if(n%i==0) return false;
    	return n!=1;
    }
    LL N;
    LL fastPow(LL x,LL n){
    	if(n==0) return 1ll;
    	LL res=fastPow(x*x%N,n>>1);
    	if(n&1) res=res*x%N;
    	return res;
    }
    void solve(){
    	bool flag=true;
    	for(LL i=2;i<N&&flag;i++) if(fastPow(i,N)!=i) flag=false;
    	if(flag) cout<<"The number "<<N<<" is a Carmichael number.
    ";
    	else cout<<N<<" is normal.
    ";
    }
    int main(){
    //	freopen("Arutoria.txt","r",stdin);
    	while(cin>>N,N){
    		if(isPrime(N)) cout<<N<<" is normal.
    ";
    		else solve();
    	}
    	return 0;
    }
    
  • 相关阅读:
    C#学习笔记
    Visual Studio 快捷键
    java 8 中lambda表达式学习
    Spfa算法
    dijkstra算法
    topSort
    并查集--学习详解
    trie树--详解
    POJ1988 并查集的使用
    Mybatis的一级缓存和二级缓存
  • 原文地址:https://www.cnblogs.com/yuhan-blog/p/12308774.html
Copyright © 2011-2022 走看看