扩展欧几里得
hdu2669, 解同余方程 ax+by=1
1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque> 7 #include <vector> 8 #include <queue> 9 #include <string> 10 #include <cstring> 11 #include <map> 12 #include <stack> 13 #include <set> 14 //#define OPEN_FILE 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 int x, y, a, b; 18 int gcd(int a, int b, int& x, int& y) 19 { 20 if(b == 0){ 21 x = 1; 22 y = 0; 23 return a; 24 } 25 else{ 26 int temp = gcd(b, a % b, x, y); 27 int t = y; 28 y = x - y * (a / b); 29 x = t; 30 return temp; 31 } 32 } 33 34 int main() 35 { 36 #ifdef OPEN_FILE 37 freopen("in.txt", "r", stdin); 38 freopen("out.txt", "w", stdout); 39 #endif // OPEN_FILE 40 while(~scanf("%d%d", &a, &b)){ 41 if(gcd(a, b ,x ,y) > 1){ 42 printf("sorry\n"); 43 continue; 44 } 45 while(x < 0){ 46 x += b; 47 y -= a; 48 } 49 printf("%d %d\n", x, y); 50 } 51 52 }
费马小定理 gcd(a,p)=1,那么 a^(p-1) ≡1(mod p)
hdu4828,费马小定理计算卡特兰数
1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque> 7 #include <vector> 8 #include <queue> 9 #include <string> 10 #include <cstring> 11 #include <map> 12 #include <stack> 13 #include <set> 14 //#define OPEN_FILE 15 #define INF 0x3f3f3f3f 16 #define MAXN 2000001 17 #define MOD 1000000007 18 using namespace std; 19 20 long long f[MAXN + 5]; 21 long long x, y; 22 long long gcd(long long a, long long b, long long& x, long long& y) 23 { 24 if(b == 0){ 25 x = 1; 26 y = 0; 27 return a; 28 } 29 else{ 30 long long temp = gcd(b, a % b, x, y); 31 long long t = y; 32 y = x - y * (a / b); 33 x = t; 34 return temp; 35 } 36 } 37 int main() 38 { 39 #ifdef OPEN_FILE 40 freopen("in.txt", "r", stdin); 41 freopen("out.txt", "w", stdout); 42 #endif // OPEN_FILE 43 int T, n; 44 scanf("%d", &T); 45 f[1] = 1; 46 for(long long i = 2; i <= MAXN; i++){ 47 f[i] = (f[i - 1] * (4 * i - 2)) % MOD; 48 gcd(i + 1, MOD, x, y); 49 if(x < 0){ 50 x = MOD - (-x) % MOD; 51 } 52 x = x % MOD; 53 f[i] = (f[i] * x) % MOD; 54 } 55 for(int cas = 1; cas <= T; cas++){ 56 scanf("%d", &n); 57 printf("Case #%d:\n%I64d\n", cas, f[n]); 58 } 59 }
中国剩余定理
hdu1573,不互素的中国剩余定理
1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque> 7 #include <vector> 8 #include <queue> 9 #include <string> 10 #include <cstring> 11 #include <map> 12 #include <stack> 13 #include <set> 14 //#define OPEN_FILE 15 #define INF 0x3f3f3f3f 16 #define M 11 17 using namespace std; 18 19 long long a[M], b[M]; 20 long long x, y; 21 bool flag; 22 long long gcd(long long a, long long b) 23 { 24 if (b == 0){ 25 return a; 26 } 27 return gcd(b, a % b); 28 } 29 30 long long exgcd(long long a, long long b, long long & x, long long & y) 31 { 32 if (b == 0){ 33 x = 1; 34 y = 0; 35 return a; 36 } 37 else{ 38 long long temp = exgcd(b, a % b, x, y); 39 long long t = y; 40 y = x - y * (a / b); 41 x = t; 42 return temp; 43 } 44 } 45 int main() 46 { 47 #ifdef OPEN_FILE 48 freopen("in.txt", "r", stdin); 49 //freopen("out.txt", "w", stdout); 50 #endif // OPEN_FILE 51 long long T, d, n, m, t, A, B; 52 scanf("%I64d", &T); 53 for (int cas = 1; cas <= T; cas++){ 54 flag = false; 55 scanf("%I64d%I64d", &n, &m); 56 for (int i = 1; i <= m; i++){ 57 scanf("%d", &a[i]); 58 //t = (t * a[i]) / gcd(t, a[i]); 59 } 60 for (int i = 1; i <= m; i++){ 61 scanf("%I64d", &b[i]); 62 } 63 A = a[1], B = b[1]; 64 for (int i = 2; i <= m; i++){ 65 d = exgcd(A, a[i], x, y); 66 if ((b[i] - B) % d != 0){ 67 flag = true; 68 break; 69 } 70 x = (b[i] - B) / d * x; 71 y = a[i] / d; 72 x = (x % y + y) % y; 73 B = x * A + B; 74 A = (A * a[i]) / d; 75 B = (B % A + A) % A; 76 } 77 if (B > n || flag){ 78 printf("0\n"); 79 } 80 else{ 81 t = 1 + (n - B) / A; 82 if (B == 0){ 83 --t; 84 } 85 printf("%I64d\n", t); 86 } 87 } 88 }
欧拉函数
求1-n与n互质的个数
1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque> 7 #include <vector> 8 #include <queue> 9 #include <string> 10 #include <cstring> 11 #include <map> 12 #include <stack> 13 #include <set> 14 //#define OPEN_FILE 15 #define INF 0x3f3f3f3f 16 #define MAXN 32770 17 using namespace std; 18 int x, n; 19 20 int euler(int x){ 21 int ans = x, m = n; 22 for(int i = 2; i * i <= x; i++){ 23 if(x % i == 0){ 24 ans = ans / i * (i - 1); 25 while(x % i == 0){ 26 x /= i; 27 } 28 } 29 } 30 if(x > 1){ 31 ans = ans / x * (x - 1); 32 } 33 return ans; 34 } 35 int main() 36 { 37 #ifdef OPEN_FILE 38 freopen("in.txt", "r", stdin); 39 freopen("out.txt", "w", stdout); 40 #endif // OPEN_FILE 41 int t, cnt; 42 scanf("%d", &n); 43 for(int i = 1; i <= n; i++){ 44 scanf("%d", &x); 45 printf("%d\n", euler(x)); 46 } 47 }
指数循环节