UVA - 10006 - Carmichael Numbers (快速幂+素数判断)

来源:互联网 发布:扇贝背单词软件 编辑:程序博客网 时间:2024/05/02 02:12


题目传送:UVA - 10006




思路:就是快速幂暴力过去就行了,然后要注意点细节,就是快速幂的时候会爆int,然后就是先判断是否为素数,是素数就直接输出结果is normal,不然会超时


AC代码:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>#include <queue>#include <stack>#include <vector>#include <map>#include <set>#include <deque>#include <cctype>#define LL long long#define INF 0x7fffffffusing namespace std;int n;int is_prime(int x) {if(x == 1) return 0;if(x == 2 || x == 3) return 1;for(int i = 2; i <= sqrt(x); i ++) {if(x % i == 0) return 0;}return 1;}int kmod(int x, int mod) {LL ret = 1;int tt = x;int t = mod;while(t) {if(t & 1) ret = (ret * x) % mod;x = ((LL)x * x) % mod;//这里会爆int t >>= 1;}if(ret == tt) return 0;else return 1; }int judge(int x) {for(int i = 2; i < n; i++) {if(kmod(i, n)) return 0;}return 1;}int main() {while(scanf("%d", &n) != EOF) {if(n == 0) break;if(!is_prime(n) && judge(n)) {//判断素数放前面,自以为没多大影响,结果TLE了一下 printf("The number %d is a Carmichael number.\n", n);}else {printf("%d is normal.\n", n);}}return 0;}













0 0
原创粉丝点击