20250726 总结
_little_duck_ · · 个人记录
A.AtCoder-abc250_e
题意
给定两个长度为
对于每个
- 如果
A 序列前x_i 项的值集合与B 序列前y_i 项的值集合相同,则输出Yes;否则输出No。
思路
应为题目要求对比对比集合,而时间复杂度为
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10;
int n, q;
int a[N], b[N];
map<int, bool> m;
map<int, int> rec;
signed main(){
random_device rd;
mt19937 reg(rd());
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
if(!rec[a[i]]){
rec[a[i]] = reg();
}
a[i] = rec[a[i]];
}
for(int i = 1; i <= n; i++){
cin >> b[i];
if(!rec[b[i]]){
rec[b[i]] = reg();
}
b[i] = rec[b[i]];
}
for(int i = 1; i <= n; i++){
if(m[a[i]] == 1){
a[i] = a[i - 1];
continue;
}
m[a[i]] = 1;
a[i] ^= a[i - 1];
}
m.clear();
for(int i = 1; i <= n; i++){
if(m[b[i]] == 1){
b[i] = b[i - 1];
continue;
}
m[b[i]] = 1;
b[i] ^= b[i - 1];
}
cin >> q;
for(int i = 1, x, y; i <= q; i++){
cin >> x >> y;
if(a[x] == b[y]){
cout << "Yes\n";
}
else{
cout << "No\n";
}
}
return 0;
}
B.AtCoder abc339_f
题意
给你
求满足以下条件的整数
-
1 \le i, j, k \le N -
A_i \times A_j = A_k
思路
考虑等式左右两边,左边只有
::::::warning[注意] 单模哈希被卡风险很大,建议采取二(三)模哈希和大质数作为模数。 ::::::
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1010, mod1 = 114514 * 1e3 + 7, mod2 = 318458931;
int n, rec[5][N][N], a[5][N];
string s[N];
map<vector<int>, int> m;
int get(int x, int mod){
int sum = 0;
for(char i : s[x]){
sum = (sum * 10 + i - '0') % mod;
}
return sum;
}
signed main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> s[i];
a[1][i] = get(i, mod1);
a[2][i] = get(i, mod2);
}
for(int i = 1; i <= n; i++){
int x1 = a[1][i], x2 = a[2][i];
m[{x1, x2}]++;
for(int j = 1; j <= n; j++){
int y1 = a[1][j], y2 = a[2][j];
rec[1][i][j] = (x1 * y1) % mod1;
rec[2][i][j] = (x2 * y2) % mod2;
}
}
int ans = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
ans += m[{rec[1][i][j], rec[2][i][j]}];
}
}
cout << ans;
return 0;
}
C.洛谷 P2312
题意
已知多项式方程:
给定
思路
通过注意数据范围可得,
对于输入的 MOD 并模 MOD 变为整数。MOD 为模数。
在计算方程时,要边计算答案便取模。底数也要同时递推求,否则会超时。
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 110, mod = 1e9 + 7;
int n, m, a[N];
string s;
vector<int> res;
int get(){
int sum = 0, f = 1;
for(char i : s){
if(i == '-'){f = -1; continue;}
sum = (sum * 10 + i - '0') % mod;
}
sum *= f;
sum = (sum + mod) % mod;
return sum;
}
signed main(){
cin >> n >> m;
for(int i = 0; i <= n; i++){
cin >> s;
a[i] = get();
}
for(int i = 1; i <= m; i++){
int x = 1, ans = 0;
for(int j = 0; j <= n; j++){
ans += (a[j] * x) % mod;
ans %= mod;
x *= i;
x %= mod;
}
if(ans == 0){
res.push_back(i);
}
}
cout << res.size() << "\n";
for(int i : res){
cout << i << "\n";
}
return 0;
}