UVA10006 - Carmichael Numbers(筛选构造素数表+快速幂)
2014-11-09 14:46
471 查看
UVA10006 - Carmichael Numbers(筛选构造素数表+快速幂)
题目链接
题目大意:如果有一个合数,然后它满足任意大于1小于n的整数a, 满足a^n%n = a;这样的合数叫做Carmichael Numbers。题目给你n,然你判断是不是Carmichael Numbers。
解题思路:首先用筛选法构造素数表,判断n是否是合数,然后在用快速幂求a^2-a^(n - 1)是否满足上述的式子。快速幂的时候最好用long long ,防止相乘溢出。
代码:
题目链接
题目大意:如果有一个合数,然后它满足任意大于1小于n的整数a, 满足a^n%n = a;这样的合数叫做Carmichael Numbers。题目给你n,然你判断是不是Carmichael Numbers。
解题思路:首先用筛选法构造素数表,判断n是否是合数,然后在用快速幂求a^2-a^(n - 1)是否满足上述的式子。快速幂的时候最好用long long ,防止相乘溢出。
代码:
#include <cstdio> #include <cstring> #include <cmath> const int maxn = 65000 + 5; typedef long long ll; int notprime[maxn]; void init () { for (int i = 2; i < maxn; i++) for (int j = 2 * i; j < maxn; j += i) notprime[j] = 1; } ll powmod(ll x, ll n, ll mod) { if (n == 1) return x; ll ans = powmod(x, n / 2, mod); ans = (ans * ans) % mod; if (n % 2 == 1) ans *= x; return ans % mod; } bool is_carmichael(int n) { for (int i = 2; i < n; i++) { if (powmod(i, n, n) != i) return false; } return true; } int main () { init(); int n; while (scanf ("%d", &n) && n) { if (notprime == 0) printf ("%d is normal.\n", n); else { bool flag = is_carmichael(n); if (flag) printf ("The number %d is a Carmichael number.\n", n); else printf ("%d is normal.\n", n); } } return 0; }
相关文章推荐
- UVA10006 - Carmichael Numbers(筛选构造素数表+高速幂)
- UVa 11752 (素数筛选 快速幂) The Super Powers
- Carmichael Numbers - UVa 10006 素数判断
- UVA - 10006 Carmichael Numbers(素数筛+快速幂)
- Uva 10006 - Carmichael Numbers【素数+快速幂取模】
- UVa 10006 Carmichael Numbers 【快速幂+素数打表(埃氏筛法)】
- uva 10006 Carmichael Numbers(快速幂+素数)
- UVA - 11752 The Super Powers 【预处理+素数筛选】
- 素数筛选法 O(n) O(nlgn) uva10357 选择与除法
- UVA - 1404 Prime k-tuple (素数筛选)
- UVa 10539 - Almost Prime Numbers(筛选求素数)
- uva 10006 素数打表+幂取模(反复平方法)(O(logn))
- UVA 10006 - Carmichael Numbers (快速幂+筛素数)
- 筛选法构造素数表
- UVA 10006 Carmichael Numbers(数论+快速幂)
- UVA10006->素数筛&&快速幂取模
- UVA 10006 Carmichael Numbers (伪素数)
- UVa 1644 - Prime Gap(筛选求素数)
- UVa10006 Carmichael Numbers【素数判定+快速模幂】
- UVA583 UVALive5406 Prime Factors【素数因子+筛选法】