20191101
A
题意
求一个三元组\((a,b,c)\),使得 \(a * b * c \% p = 1\)。
做法
暴力记录有每一个模值多少个数,枚举即可。
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize(3, "Ofast")
#define int long long
map <int,int> p;
map <int,int> g;
map <int,int> t;
int n;
int f[3000]; int pl;
int P;
int inv;
int getinv(int x){
int xt = x;
int ret = 1;
int base = P - 2;
while (base >= 1){
if (base & 1) ret = xt * ret % P;
xt = xt * xt % P;
base /= 2;
}
return ret;
}
signed main(){
freopen("awesome.in","r",stdin);
freopen("awesome.out","w",stdout);
cin >> n >> P;
int ans = 0;
for (int i=1;i<=n;i++){
int tp;
cin >> tp;
if (tp % P == 0) continue;
p[tp] ++;
// if (p[tp] == 3){
// if (tp * tp % P * tp % P == 1) ans ++; // deal with 3
// }
}
typedef map<int,int>::iterator Iter;
for (Iter it = p.begin(); it != p.end(); it ++){
f[++pl] = it -> first;
}
for (int i=1;i<=pl;i++){
t[f[i] % P] ++;
}
for (int j=pl;j>=1;j--){
for (int i=1;i<j;i++){
int inv = getinv(f[i] * f[j] % P);
if (g.count(inv)) ans += g[inv];
}
if (p[f[j]] != 1){
int inv = getinv(f[j] * f[j] % P);
if (t.count(inv)) ans += t[inv];//, cout << f[j] << ' ' << f[j] << endl;
if (p[f[j]] == 2 && f[j] * f[j] % P * f[j] % P == 1) ans --;//, cout << '?' << endl;
}
g[f[j] % P] ++;
} // deal with 1 & 2 & 3
cout << ans << endl;
}
B
最长上升子序列Ex
二分时改一下边界即可
关键代码:
int n = read();
for (int i = 1; i <= n; ++i)
its[i].wei = read(), its[i].val = read();
sort(its + 1, its + n + 1);
int m = read();
for (int i = 1; i <= m; ++i)
lim[i] = read();
sort(lim + 1, lim + m + 1, Comp);
for (int i = 1; i <= n + 2; ++i)
_max[i] = 0;
int ans = 0;
for (int i = 1; i <= n && ans <= m; ++i){
int l = 1, r = i, mid, bans = 0;
while (l <= r){
mid = l + r >> 1;
if (_max[mid] >= its[i].val) l = mid + 1, bans = mid;
else r = mid - 1;
}
f[i] = bans + 1;
if (its[i].wei > lim[f[i]])
f[i] = -1000000007;
if (f[i] > ans) ans = f[i];
if (f[i] > 0){
if (_max[f[i]] < its[i].val)
_max[f[i]] = its[i].val;
}
}
C
一道动归题。
令 \(f_{i,j}\) 表示当子树大小为 \(i\) 时深度小于等于 \(j\) 的数量。
对于一个新的点, \(f_{i,j} = \sum_k f_{k,j-1} C_{i-2}^{k-1} * f_{i-k,j}\)
边界: \(f_{1,i} = 1\)
关键代码:
for (int i = 1; i <= n; i++)
f[1][i] = 1;
for (int i = 2; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= i - 1; k++)
if (!ban[k])
f[i][j] = (f[i][j] + 1ll * f[k][j - 1] * C(i - 2, k - 1) % Mod * f[i - k][j]) % Mod;
Copyright (c) dgklr : Creative Commons - BY - NC