1576

const int mod = 9973;

n = a - a / mod * mod;

a / b = ans;

ans * b = a = a / mod * mod + n;

n = b * ans - a / mod * mod;

n = b * ans + mod * y;

extended_gcd(b, mod, ans, y);

 1 #define PRON "hdu1576"
 2 #define LL "%lld"
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <iostream>
 6 #include <algorithm>
 7 using namespace std;
 8 typedef long long ll;
 9 
10 const int MOD = 9973;
11 
12 int Tcase;
13 
14 ll extended_gcd(ll a, ll b, ll & x, ll & y){
15     if (b == 0){
16         x = 1, y = 0;
17         return a;
18     }
19 
20     ll d = extended_gcd(b, a % b, x, y);
21     ll temp = x;
22     x = y;
23     y = temp - a / b * y;
24     
25     return d;
26 }
27 
28 int main(){
29 #ifndef ONLINE_JUDGE
30     freopen(PRON ".in", "r", stdin);
31 #endif
32 
33     ll a, b, x, y;
34 
35     scanf("%d", &Tcase);
36     while (Tcase --){
37         scanf(LL LL, &a, &b);
38         extended_gcd(b, MOD, x, y);
39         x = ((x * a % MOD) + MOD) % MOD;
40         printf(LL "\n", x);
41     }
42 }
hdu1576

  

2824

Σphi[i]

 1 #define PRON "hdu2824"
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 typedef long long ll;
 8 
 9 const int MAXN = 3000000;
10 
11 int n, m;
12 ll phi[MAXN + 10];
13 
14 void get_phi(){
15     memset(phi, 0, sizeof phi);
16     phi[1] = 1;
17     for (int i = 2; i <= MAXN; i ++)
18         if (!phi[i]){
19             for (int j = i; j <= MAXN; j += i){
20                 if (!phi[j])
21                     phi[j] = j;
22                 phi[j] = phi[j] / i * (i - 1);
23             }
24         }
25 }
26 
27 int main(){
28 #ifndef ONLINE_JUDGE
29     freopen(PRON ".in", "r", stdin);
30 #endif
31 
32     get_phi();
33     for (int i = 1; i <= MAXN; i ++)
34         phi[i] += phi[i - 1];
35 
36     while (scanf("%d %d", &n, &m) == 2)
37         cout << phi[m] - phi[n - 1] << endl;
38 }
hdu2824

 

1573

中国剩余定理的一般形式

 1 #define PRON "hdu1573"
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 typedef int ll;
 8 
 9 const int MAXN = 10 + 5;
10 
11 int Tcase, _max, n, a[MAXN], b[MAXN];
12 
13 ll extended_gcd(ll a, ll b, ll & x, ll & y){
14     if (b == 0){
15         x = 1, y = 0;
16         return a;
17     }
18 
19     ll d = extended_gcd(b, a % b, x, y);
20     ll temp = x;
21     x = y; 
22     y = temp - a / b * y;
23 
24     return d;
25 }
26 
27 ll normal_crt(){
28     ll m1, m2, r1, r2, x, y;
29 
30     //solve N = r1 (mod m1)
31     //      N = r2 (mod m2)
32     m1 = a[0], r1 = b[0];
33     for (int i = 1; i < n; i ++){
34         m2 = a[i], r2 = b[i];
35     
36         //solve d = x * m1 + y * m2
37         //(x, y) is the solution to the equation above
38         //solve c = r2 - r1 = y * m2 - x * m1
39         //(x0, y0) is the solution to the equation above
40         //x0 = x * c / d, y0 = x * c / d
41         ll d = extended_gcd(m1, m2, x, y);
42         ll c = r2 - r1;
43         if (c % d)
44             return 0;
45 
46         ll t = m2 / d;
47         x = (x * c / d % t + t) % t;
48         
49         //r1 is the solution to the equaions from 1st to ith
50         r1 += m1 * x;
51         //m1 is the lcm of m1 to mi
52         m1 *= t;
53     }
54 
55     if (_max < r1)
56         return 0;
57 
58     //if (x0, y0) is one of the solution
59     //(x0 + k * m2 / d, y0 - k * m1 / d) (k -> Z) also apply
60     return (_max - r1) / m1 + 1 - (bool)(r1 == 0);
61 }
62 
63 int main(){
64 #ifndef ONLINE_JUDGE
65     freopen(PRON ".in", "r", stdin);
66 #endif
67 
68     scanf("%d", &Tcase);
69     while (Tcase --){
70         scanf("%d %d", &_max, &n);
71         for (int i = 0; i < n; i ++)
72             scanf("%d", a + i);
73         for (int i = 0; i < n; i ++)
74             scanf("%d", b + i);
75 
76         printf("%d\n", normal_crt());
77     }
78 }
1573

 

1370

中国剩余定理 

 1 #define PRON "hdu1370"
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 typedef int ll;
 8 
 9 const int MAXN = 10;
10 const int MOD = 21252;
11 
12 int Tcase, cnt, st, a[MAXN], b[MAXN];
13 
14 ll extended_gcd(ll a, ll b, ll & x, ll & y){
15     if (b == 0){
16         x = 1, y = 0;
17         return a;
18     }
19 
20     ll d = extended_gcd(b, a % b, x, y);
21     ll temp = x;
22     x = y;
23     y = temp - a / b * y;
24 
25     return d;
26 }
27 
28 ll inv(ll a, ll n){
29     ll x, y;
30     ll d = extended_gcd(a, n, x, y);
31     return d == 1 ? (x + n) % n : -1;
32 }
33 
34 ll crt(int n){
35     ll ret = 0, m = 1;
36     
37     for (int i = 0; i < n; i ++)
38         a[i] %= b[i], m *= b[i]; 
39 
40     for (int i = 0; i < n; i ++)
41         ret = (ret + a[i] * (m / b[i]) * inv(m / b[i], b[i])) % m;
42 
43     ret -= st;
44     return ret + MOD * (bool)(ret <= 0);
45 }
46 
47 int main(){
48 #ifndef ONLINE_JUDGE
49     freopen(PRON ".in", "r", stdin);
50 #endif
51 
52     cnt = 0;
53     b[0] = 23, b[1] = 28, b[2] = 33;
54 
55     scanf("%d", &Tcase);
56     while (scanf("%d %d %d %d", &a[0], &a[1], &a[2], &st) == 4 && !(a[0] == -1 && a[1] == -1 && a[2] == -1))
57         printf("Case %d: the next triple peak occurs in %d days.\n", ++ cnt, crt(3));
58 }
1370

 

Posted on 2016-09-28 16:47  cjhahaha  阅读(161)  评论(0编辑  收藏  举报