HDU Math Problems

时间:2023-03-08 16:20:18

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);

 #define PRON "hdu1576"
#define LL "%lld"
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll; const int MOD = ; int Tcase; ll extended_gcd(ll a, ll b, ll & x, ll & y){
if (b == ){
x = , y = ;
return a;
} ll d = extended_gcd(b, a % b, x, y);
ll temp = x;
x = y;
y = temp - a / b * y; return d;
} int main(){
#ifndef ONLINE_JUDGE
freopen(PRON ".in", "r", stdin);
#endif ll a, b, x, y; scanf("%d", &Tcase);
while (Tcase --){
scanf(LL LL, &a, &b);
extended_gcd(b, MOD, x, y);
x = ((x * a % MOD) + MOD) % MOD;
printf(LL "\n", x);
}
}

hdu1576

2824

Σphi[i]

 #define PRON "hdu2824"
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll; const int MAXN = ; int n, m;
ll phi[MAXN + ]; void get_phi(){
memset(phi, , sizeof phi);
phi[] = ;
for (int i = ; i <= MAXN; i ++)
if (!phi[i]){
for (int j = i; j <= MAXN; j += i){
if (!phi[j])
phi[j] = j;
phi[j] = phi[j] / i * (i - );
}
}
} int main(){
#ifndef ONLINE_JUDGE
freopen(PRON ".in", "r", stdin);
#endif get_phi();
for (int i = ; i <= MAXN; i ++)
phi[i] += phi[i - ]; while (scanf("%d %d", &n, &m) == )
cout << phi[m] - phi[n - ] << endl;
}

hdu2824

1573

中国剩余定理的一般形式

 #define PRON "hdu1573"
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef int ll; const int MAXN = + ; int Tcase, _max, n, a[MAXN], b[MAXN]; ll extended_gcd(ll a, ll b, ll & x, ll & y){
if (b == ){
x = , y = ;
return a;
} ll d = extended_gcd(b, a % b, x, y);
ll temp = x;
x = y;
y = temp - a / b * y; return d;
} ll normal_crt(){
ll m1, m2, r1, r2, x, y; //solve N = r1 (mod m1)
// N = r2 (mod m2)
m1 = a[], r1 = b[];
for (int i = ; i < n; i ++){
m2 = a[i], r2 = b[i]; //solve d = x * m1 + y * m2
//(x, y) is the solution to the equation above
//solve c = r2 - r1 = y * m2 - x * m1
//(x0, y0) is the solution to the equation above
//x0 = x * c / d, y0 = x * c / d
ll d = extended_gcd(m1, m2, x, y);
ll c = r2 - r1;
if (c % d)
return ; ll t = m2 / d;
x = (x * c / d % t + t) % t; //r1 is the solution to the equaions from 1st to ith
r1 += m1 * x;
//m1 is the lcm of m1 to mi
m1 *= t;
} if (_max < r1)
return ; //if (x0, y0) is one of the solution
//(x0 + k * m2 / d, y0 - k * m1 / d) (k -> Z) also apply
return (_max - r1) / m1 + - (bool)(r1 == );
} int main(){
#ifndef ONLINE_JUDGE
freopen(PRON ".in", "r", stdin);
#endif scanf("%d", &Tcase);
while (Tcase --){
scanf("%d %d", &_max, &n);
for (int i = ; i < n; i ++)
scanf("%d", a + i);
for (int i = ; i < n; i ++)
scanf("%d", b + i); printf("%d\n", normal_crt());
}
}

1573

1370

中国剩余定理

 #define PRON "hdu1370"
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef int ll; const int MAXN = ;
const int MOD = ; int Tcase, cnt, st, a[MAXN], b[MAXN]; ll extended_gcd(ll a, ll b, ll & x, ll & y){
if (b == ){
x = , y = ;
return a;
} ll d = extended_gcd(b, a % b, x, y);
ll temp = x;
x = y;
y = temp - a / b * y; return d;
} ll inv(ll a, ll n){
ll x, y;
ll d = extended_gcd(a, n, x, y);
return d == ? (x + n) % n : -;
} ll crt(int n){
ll ret = , m = ; for (int i = ; i < n; i ++)
a[i] %= b[i], m *= b[i]; for (int i = ; i < n; i ++)
ret = (ret + a[i] * (m / b[i]) * inv(m / b[i], b[i])) % m; ret -= st;
return ret + MOD * (bool)(ret <= );
} int main(){
#ifndef ONLINE_JUDGE
freopen(PRON ".in", "r", stdin);
#endif cnt = ;
b[] = , b[] = , b[] = ; scanf("%d", &Tcase);
while (scanf("%d %d %d %d", &a[], &a[], &a[], &st) == && !(a[] == - && a[] == - && a[] == -))
printf("Case %d: the next triple peak occurs in %d days.\n", ++ cnt, crt());
}

1370