Skip to content

Commit

Permalink
add knowledge
Browse files Browse the repository at this point in the history
  • Loading branch information
devilran6 committed Oct 5, 2023
1 parent 3d82d8f commit 9e60880
Show file tree
Hide file tree
Showing 6 changed files with 576 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
125 changes: 125 additions & 0 deletions docs/ACM/Knowledge/Math/其他/xor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
## 技巧

- 拆位
- 公式

## 牛客第七场 C Beautiful Sequence

[题目链接](https://ac.nowcoder.com/acm/contest/57361/C)

![](assets/image-20230807195320433.png)

![](assets/2023-10-06-01-38-28.png)

```cpp
#include <bits/stdc++.h>

#define LL long long
#define ULL unsigned long long
#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int INF = 0x3f3f3f3f;

void solve()
{
int n, k;
cin >> n >> k;

vector<int> b(n + 1), w(n + 1);
for (int i = 1; i < n; i++)
{
cin >> b[i];
w[i + 1] = w[i] ^ b[i];
}

vector<int> a1(30, -1);
for (int i = 1; i < n; i++)
{
int u = -1;

for (int j = 29; j >= 0; j--)
if ((b[i] >> j) & 1)
{
u = j;
break;
}

// 当 Bi == 0 的时候直接跳过即可
if (u == -1)
continue;
// ==> a[i]的第u位为0
// ==> a[1]的第u位为t

int t1 = ((w[i] >> u) & 1) ^ 0;

if (a1[u] != -1 && a1[u] != t1)
{
cout << "-1" << endl;
return;
}
else
a1[u] = t1;
}

// for (int i = 29; i >= 0; i--)
// cerr << a1[i] << endl;

int cnt = 0;
for (int i = 0; i < a1.size(); i++)
if (a1[i] == -1)
cnt++;

if (pow(2, cnt) < k)
{
cout << "-1" << endl;
return;
}
k--;
while (k)
{
int c = k & 1;
k >>= 1;
for (int i = 0; i <= 29; i++)
if (a1[i] == -1)
{
a1[i] = c;
break;
}
}

int ans = 0;
for (int i = 29; i >= 0; i--)
if (a1[i] == -1)
a1[i] = 0;
for (int i = 29; i >= 0; i--)
ans = ans * 2 + a1[i];

cout << ans << " ";
for (int i = 2; i <= n; i++)
{
cout << (ans ^ w[i]) << " \n"[i == n];
}
return;
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);

int t;
cin >> t;
while (t--)
{
solve();
}

return 0;
}
```
225 changes: 225 additions & 0 deletions docs/ACM/Knowledge/Math/数论/扩展欧几里得.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,229 @@ LL exgcd(LL a, LL b, LL &x, LL &y)

return d;
}
```
## 牛客第一场 M Water
[题目链接](https://ac.nowcoder.com/acm/contest/57355/M)
[证明链接](https://zhuanlan.zhihu.com/p/644323896)
四种操作 两个水杯 容量A、B 凑喝水的量x
exgcd uA + vB = x
if(u >= 0 && v >= 0) 直接两个杯子倒喝倒喝就行 ans = 2(u+v)
if(u < 0 || v < 0) 可以构造 ans = 2abs(u)+2abs(v)-1
if(u < 0 && v < 0) 不存在哦
> 严格证明最少这么多操作比较复杂
```cpp
#include <bits/stdc++.h>
#define LL long long
#define ULL unsigned long long
#define x first
#define y second
#define endl "\n"
using namespace std;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int INF = 0x3f3f3f3f;
LL exgcd(LL a, LL b, LL &x, LL &y)
{
if (b == 0)
{
x = 1, y = 0;
return a;
}
LL g = exgcd(b, a % b, y, x);
y -= a / b * x;
return g;
}
LL get_ans(LL a, LL b)
{
if (a >= 0 && b >= 0)
{
return 2 * (a + b);
}
return 2 * abs(a) + 2 * abs(b) - 1;
}
void solve()
{
LL A, B, x;
cin >> A >> B >> x;
if (A > B)
swap(A, B);
LL u, v;
LL g = exgcd(A, B, u, v); // uA + vB = g
A /= g, B /= g;
if (x % g != 0)
{
cout << -1 << endl;
return;
}
x /= g;
// uA + vB = 1 ==> uA + vB = x
u = (u * x % B + B) % B; // 获得大于0的最小的u
v = (x - u * A) / B;
LL ans = 1E18;
ans = min(ans, get_ans(u, v));
ans = min(ans, get_ans(u - B, v + A));
v = (v % A + A) % A;
u = (x - v * B) / A;
ans = min(ans, get_ans(u, v));
ans = min(ans, get_ans(u + B, v - A));
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
```
## 杭电第八场 题3 Congruences

[题目链接](https://vjudge.net/problem/HDU-7363)

```cpp
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <array>
#include <numeric>

#define LL long long
#define ULL unsigned long long
#define x first
#define y second
#define endl "\n"

using namespace std;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int INF = 0x3f3f3f3f;

LL qmi(LL a, LL k, LL p)
{
if (a == 0)
return 0;
LL res = 1;
while (k)
{
if (k & 1)
res = (__int128)res * a % p;
a = (__int128)a * a % p;
k >>= 1;
}
return res;
}

LL exgcd(LL a, LL b, LL &x, LL &y)
{
if (b == 0)
{
x = 1, y = 0;
return a;
}

LL g = exgcd(b, a % b, y, x);
y -= (__int128)a / b * x;

return g;
}

void solve()
{
LL n, m;
cin >> m;

vector<LL> p(m + 1), q(m + 1);
LL M = 1;
for (int i = 0; i < m; i++)
{
cin >> p[i] >> q[i];
M *= p[i];
}

LL ans = 0;
for (int i = 0; i < m; i++)
{
LL Mi = M / p[i];
LL ti, x;
exgcd(Mi, p[i], ti, x);
ans = ((__int128)ans + (__int128)q[i] * Mi * ti % M) % M;
}

ans = (ans % M + M) % M;

bool ok = true;
for (int i = 0; i < m; i++)
{
LL lhs = qmi(ans, p[i], M);
if (lhs != q[i])
{
ok = false;
break;
}
}

if (ans == 0)
ans = M;

if (!ok)
ans = -1;
cout << ans << endl;

return;
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);

int t;
cin >> t;
while (t--)
{
solve();
}

return 0;
}
```
Loading

0 comments on commit 9e60880

Please sign in to comment.