A
判断一个数有没有奇数因子。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
int main()
{
int T;
scanf("%d", &T);
while(T)
{
T--;
LL n;
scanf("%lld", &n);
while(n % 2 == 0)
n /= 2;
if(n == 1)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}
B
判断一个数能不能拆分成2020和2021的和。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
int main()
{
int T;
scanf("%d", &T);
while(T)
{
T--;
int n;
scanf("%d", &n);
int t = n / 2020;
int m = n % 2020;
if(m <= t)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
C
给一些二元组,选取两个二元组,对应位置不冲突的方案数。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int a[N], b[N], cnta[N], cntb[N];
int main()
{
int T;
scanf("%d", &T);
while(T)
{
T--;
int n, m, k;
LL ans = 0;
scanf("%d%d%d", &n, &m, &k);
for(int i = 1; i <= k; i++)
{
scanf("%d", &a[i]);
cnta[a[i]]++;
}
for(int i = 1; i <= k; i++)
{
scanf("%d", &b[i]);
cntb[b[i]]++;
}
for(int i = 1; i <= k; i++)
ans += k - 1 - (cnta[a[i]] - 1) - (cntb[b[i]] - 1);
printf("%lld\n", ans / 2);
for(int i = 1; i <= n; i++)
cnta[i] = 0;
for(int i = 1; i <= m; i++)
cntb[i] = 0;
}
return 0;
}
D
两类物品,一类价值为1,一类为2,每件物品占据一定空间。共有n件物品,求一种组合使得总空间至少为m,价值最小。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
const int inf = 1e9 + 7;
LL sum1[N], sum2[N], st1[N], st2[N], a[N];
int b[N];
int cmp(LL x, LL y){ return x > y;}
int main()
{
int T;
scanf("%d", &T);
while(T)
{
T--;
int n;
LL m;
scanf("%d%lld", &n, &m);
for(int i = 1; i <= n; i++)
scanf("%lld", &a[i]);
int cnt1 = 0, cnt2 = 0;
for(int i = 1; i <= n; i++)
{
scanf("%d", &b[i]);
if(b[i] == 1)
st1[++cnt1] = a[i];
else
st2[++cnt2] = a[i];
}
sort(st1 + 1, st1 + cnt1 + 1, cmp);
sort(st2 + 1, st2 + cnt2 + 1, cmp);
for(int i = 1; i <= cnt1; i++)
sum1[i] = sum1[i - 1] + st1[i];
for(int i = 1; i <= cnt2; i++)
sum2[i] = sum2[i - 1] + st2[i];
int ans = inf;
for(int i = 0; i <= cnt1; i++)
{
int x = m - sum1[i];
int pos = lower_bound(sum2 , sum2 + cnt2 + 1, x) - sum2;
pos = min(pos, cnt2);
if(sum2[pos] + sum1[i] >= m)
ans = min(ans, i + pos * 2);
}
if(ans < inf)
printf("%d\n", ans);
else
printf("-1\n");
}
return 0;
}
E
n件物品选m件,求总价值最大的方案数。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int N = 1e3 + 10;
const int inf = 1e9 + 7;
const LL mod = 1e9 + 7;
LL f[N];
int a[N];
int cmp(int x, int y)
{
return x > y;
}
void init()
{
f[0] = 1;
for(int i = 1; i <= 1000; i++)
f[i] = f[i - 1] * (LL)i % mod;
}
LL inv(LL x)
{
int p = mod - 2;
LL c = 1;
while(p)
{
if(p & 1)
c = c * x % mod;
x = x * x % mod;
p >>= 1;
}
return c;
}
LL C(int n, int m)
{
return f[n] * inv(f[m]) % mod * inv(f[n - m]) % mod;
}
int main()
{
init();
int T;
scanf("%d", &T);
while(T)
{
T--;
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
sort(a + 1, a + n + 1, cmp);
int l, r;
for(l = m; l > 0; l--)
if(a[l] != a[m])
break;
l++;
for(r = m ; r <= n; r++)
if(a[r] != a[m])
break;
r--;
printf("%lld\n", C(r - l + 1, m - l + 1));
}
return 0;
}
F
给一个方阵,判断是否可以通过无数次整行取反或整列取反来变换成另一个方阵。
提示: 2-sat
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 1010;
struct EDGE{
int to, nxt;
EDGE(){}
EDGE(int x, int y){ to = x, nxt = y;}
}edge[N * N * 4];
int t[N * 4], dfn[N * 4], low[N * 4], in[N * 4], a[N][N], st[N * 4], col[N * 4];
char s[N][N], ss[N];
int cnt, tot, colcnt, stop;
void myclear(int n)
{
for(int i = 1; i <= n * 4; i++)
t[i] = 0, dfn[i] = 0, low[i] = 0, st[i] = 0;
cnt = 0;
tot = 0;
stop = 0;
colcnt = 0;
}
void addedge(int x, int y)
{
edge[++tot] = EDGE(y, t[x]);
t[x] = tot;
}
void dfs(int x, int fa)
{
dfn[x] = ++cnt;
low[x] = cnt;
in[x] = 1;
st[++stop] = x;
for(int p = t[x]; p; p = edge[p].nxt)
{
int y = edge[p].to;
if(y == fa)
continue;
if(dfn[y] == 0)
{
dfs(y, x);
low[x] = min(low[y], low[x]);
}
else
if(in[y])
low[x] = min(low[x], dfn[y]);
}
in[x] = 0;
if(dfn[x] == low[x])
{
colcnt--;
int y = st[stop];
do{
y = st[stop];
col[y] = colcnt;
stop--;
}while(y != x);
}
}
int main()
{
int T;
scanf("%d", &T);
while(T)
{
T--;
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%s", s[i] + 1);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
a[i][j] = s[i][j] - '0';
for(int i = 1; i <= n; i++)
scanf("%s", s[i] + 1);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(s[i][j] == '1')
a[i][j] ^= 1;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
{
if(a[i][j])
{
addedge(i, j + n + 2 * n);
addedge(j + n + 2 * n, i);
addedge(j + n, i + 2 * n);
addedge(i + 2 * n, j + n);
}
else
{
addedge(i, j + n);
addedge(j + n, i);
addedge(i + 2 * n, j + n + 2 * n);
addedge(j + n + 2 * n, i + 2 * n);
}
}
int flag = 1;
for(int i = 1; i <= n * 4; i++)
if(!dfn[i])
dfs(i, 0);
for(int i = 1; i <= n * 2; i++)
if(col[i] == col[i + 2 * n])
flag = 0;
if(flag)
printf("YES\n");
else
printf("NO\n");
myclear(n);
}
return 0;
}
G
给一个数组,求最少删掉多少个数后,任意两个数之间是倍数关系。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 2e5 + 10;
int a[N], f[N], cnt[N];
void myclear(int n)
{
for(int i = 1; i <= n; i++)
f[a[i]] = 0, cnt[a[i]] = 0;
}
int main()
{
int T;
scanf("%d", &T);
while(T)
{
T--;
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
cnt[a[i]] ++;
}
sort(a + 1, a + n + 1);
int ans = 0;
for(int i = 1; i <= n; i++)
{
if(a[i] != a[i - 1])
{
f[a[i]] = cnt[a[i]];
for(int j = 1; j * j <= a[i]; j++)
if(a[i] % j == 0)
{
if(a[i] != j)
f[a[i]] = max(f[a[i]], f[j] + cnt[a[i]]);
if(a[i] != a[i] / j)
f[a[i]] = max(f[a[i]], f[a[i] / j] + cnt[a[i]]);
}
ans = max(ans, f[a[i]]);
}
}
printf("%d\n", n - ans);
myclear(n);
}
return 0;
}