本文主要是介绍UVa 10006 - Carmichael Numbers,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
连接网址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=947
这一题的用到快速幂的求法:
代码如下
AC代码:
# include <cstdio>
# include <cmath>
using namespace std;
typedef long long int LL;
int mod_pow(int x, int n, int mod){if(n==0){return 1;}LL ans;ans=mod_pow(x, n/2, mod);ans=ans*ans%mod;if(n%2==1){ans=ans*x%mod;}return (int)ans;
}
int is_prime(int n){if(n<=1){return 0;}for(int i=2; i<=sqrt(n); i++){if(n%i==0){return 0;}}return 1;
}
int main(){int n, i, j, k, flage;while(scanf("%d", &n)){if(n==0){break;}if(is_prime(n)){printf("%d is normal.\n", n);continue;}flage=0;for(i=2; i<n; i++){if(mod_pow(i, n, n)!=i){flage=1;break;}}if(flage){printf("%d is normal.\n", n);}else{printf("The number %d is a Carmichael number.\n", n);}}return 0;
}
这篇关于UVa 10006 - Carmichael Numbers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!