Codeforces Round #697 (Div. 3) (A - G)
目录
A. Odd Divisor
判断是否是2的次幂
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <list>
#include <map>
#include <iostream>
#include <iomanip>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f
#define PI 3.1415926535898
#define F first
#define S second
#define endl '\n'
#define lson rt << 1
#define rson rt << 1 | 1
#define lowbit(x) (x & (-x))
#define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _per(i, a, b) for (int i = (a); i >= (b); --i)
using namespace std;
const int maxn = 2e3 + 7;
int n;
map<LL, bool> mp;
int init()
{
LL x = 2;
while (x <= 1e14)
{
mp[x] = true;
x *= 2;
}
return 0;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
LL n;
init();
while (t--)
{
cin >> n;
if (mp[n])
cout << "NO" << endl;
else
cout << "YES" << endl;
}
}
B. New Year's Number
如果除2020的余数<=商 则可行
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <list>
#include <map>
#include <iostream>
#include <iomanip>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f
#define PI 3.1415926535898
#define F first
#define S second
#define endl '\n'
#define lson rt << 1
#define rson rt << 1 | 1
#define lowbit(x) (x & (-x))
#define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _per(i, a, b) for (int i = (a); i >= (b); --i)
using namespace std;
const int maxn = 2e3 + 7;
int n;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
LL n;
while (t--)
{
cin >> n;
LL num = n / 2020;
LL r = n % 2020;
if (r <= num)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
C. Ball in Berland
容斥
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <list>
#include <map>
#include <iostream>
#include <iomanip>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f
#define PI 3.1415926535898
#define F first
#define S second
#define endl '\n'
#define lson rt << 1
#define rson rt << 1 | 1
#define lowbit(x) (x & (-x))
#define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _per(i, a, b) for (int i = (a); i >= (b); --i)
using namespace std;
const int maxn = 2e5 + 7;
int n, m, k;
map<pair<int, int>, int> mp;
map<int, int> numa, numb;
int a[maxn], b[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
cin >> n >> m >> k;
mp.clear(), numa.clear(), numb.clear();
LL ans = 0;
_rep(i, 1, k) cin >> a[i];
_rep(i, 1, k) cin >> b[i];
_rep(i, 1, k)
{
ans += i - 1;
ans -= numa[a[i]] + numb[b[i]];
ans += mp[{a[i], b[i]}];
numa[a[i]]++, numb[b[i]]++;
mp[{a[i], b[i]}]++;
}
cout << ans << endl;
}
}
D. Cleaning the Phone
一开始01背包T了,实际上只要枚举每个常规app的数量,二分出对应最少需要的重要app数量,取最小的。
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <list>
#include <map>
#include <iostream>
#include <iomanip>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f
#define PI 3.1415926535898
#define F first
#define S second
#define endl '\n'
#define lson rt << 1
#define rson rt << 1 | 1
#define lowbit(x) (x & (-x))
#define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _per(i, a, b) for (int i = (a); i >= (b); --i)
using namespace std;
const int maxn = 4e5 + 7;
int n, m, k;
LL a[maxn], b[maxn], suma[maxn], sumb[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
cin >> n >> m;
LL tot = 0;
vector<LL> aa, bb;
_rep(i, 1, n)
{
cin >> a[i];
tot += a[i];
}
_rep(i, 1, n)
{
cin >> b[i];
if (b[i] == 1)
aa.push_back(a[i]);
else
bb.push_back(a[i]);
}
if (tot < m)
{
cout << "-1" << endl;
continue;
}
int sza = aa.size(), szb = bb.size();
sort(aa.begin(), aa.end(), greater<int>());
sort(bb.begin(), bb.end(), greater<int>());
int ans = inf;
f(i, 0, sza)
{
suma[i + 1] = suma[i] + aa[i];
}
f(i, 0, szb) sumb[i + 1] = sumb[i] + bb[i];
_rep(i, 0, sza)
{
LL tmp = suma[i];
if (tmp >= m)
{
ans = min(ans, i);
break;
}
int pos = lower_bound(sumb + 1, sumb + szb + 1, m - tmp) - sumb;
if (pos == szb + 1)
continue;
ans = min(ans, i + 2 * pos);
}
cout << ans << endl;
}
}
E. Advertising Agency
保证和最大,即取所有数的前k大,设第k大的数为x,取法数就是C(要取的x个数,x的总数)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <list>
#include <map>
#include <iostream>
#include <iomanip>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f
#define PI 3.1415926535898
#define F first
#define S second
#define endl '\n'
#define lson rt << 1
#define rson rt << 1 | 1
#define lowbit(x) (x & (-x))
#define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _per(i, a, b) for (int i = (a); i >= (b); --i)
#define debug(x) cout << "VAL = " << x << endl;
using namespace std;
const int maxn = 1e3 + 7;
const int mod = 1e9 + 7;
int n, m, k;
int a[maxn];
LL fac[maxn], inv[maxn];
map<int, int> mp;
LL pow(long long x, int k)
{
long long ans = 1;
while (k)
{
if (k & 1)
ans = ans * x % mod;
x = x * x % mod;
k >>= 1;
}
return ans;
}
void init()
{
fac[0] = inv[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % mod;
inv[n] = pow(fac[n], mod - 2);
for (int i = n - 1; i >= 1; i--)
inv[i] = inv[i + 1] * (i + 1) % mod;
}
LL getC(int n, int m)
{
return fac[n] * inv[m] % mod * inv[n - m] % mod;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
n = 1007;
init();
cin >> t;
while (t--)
{
mp.clear();
cin >> n >> k;
_rep(i, 1, n) cin >> a[i], mp[a[i]]++;
sort(a + 1, a + 1 + n, greater<int>());
int num = a[k];
int pos = upper_bound(a + 1, a + n + 1, num, greater<int>()) - a;
int cnt = pos - k - 1;
cout << getC(mp[a[k]], cnt) << endl;
}
}
F. Unusual Matrix
如果某一列的第一个不一样,那就异或这一列,如果某一行的第一个不一样,就异或这一行,其它情况不一样则不能把a变成b。对于左上角的元素不一样,变行和变列都要试一下。
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <list>
#include <map>
#include <iostream>
#include <iomanip>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f
#define PI 3.1415926535898
#define F first
#define S second
#define endl '\n'
#define lson rt << 1
#define rson rt << 1 | 1
#define lowbit(x) (x & (-x))
#define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _per(i, a, b) for (int i = (a); i >= (b); --i)
#define debug(x) cout << "VAL = " << x << endl;
using namespace std;
const int maxn = 1e3 + 7;
const int mod = 1e9 + 7;
int n;
int a[maxn][maxn], b[maxn][maxn], c[maxn][maxn], ro[maxn], co[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
cin >> n;
int flag = 1;
char ch;
memset(ro, 0, sizeof(ro));
memset(co, 0, sizeof(co));
_rep(i, 1, n)
{
_rep(j, 1, n)
{
cin >> ch;
a[i][j] = ch - '0';
c[i][j] = a[i][j];
}
}
_rep(i, 1, n)
{
_rep(j, 1, n)
{
cin >> ch;
b[i][j] = ch - '0';
if (b[i][j] != a[i][j])
flag = 0;
}
}
if (flag)
{
cout << "YES" << endl;
continue;
}
int tmp = 1;
_rep(i, 1, n)
{
_rep(j, 1, n)
{
if ((co[j] + ro[i]) % 2)
c[i][j] ^= 1;
if (c[i][j] != b[i][j])
{
if (i == 1)
co[j]++;
else if (j == 1)
ro[i]++;
else
{
tmp = 0;
break;
}
}
}
if (!tmp)
break;
}
if (tmp)
{
flag = 1;
cout << "YES" << endl;
continue;
}
if (b[1][1] != a[1][1])
{
_rep(i, 1, n)
{
_rep(j, 1, n) c[i][j] = a[i][j];
}
tmp = 1;
_rep(i, 1, n)
{
_rep(j, 1, n)
{
if ((co[j] + ro[i]) % 2)
c[i][j] ^= 1;
if (c[i][j] != b[i][j])
{
if (j == 1)
ro[i]++;
else if (i == 1)
co[j]++;
else
{
tmp = 0;
break;
}
}
}
if (!tmp)
break;
}
flag = tmp;
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
G. Strange Beauty
dp,枚举每个因子
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <list>
#include <map>
#include <iostream>
#include <iomanip>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f
#define PI 3.1415926535898
#define F first
#define S second
#define endl '\n'
#define lson rt << 1
#define rson rt << 1 | 1
#define lowbit(x) (x & (-x))
#define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _per(i, a, b) for (int i = (a); i >= (b); --i)
using namespace std;
const int maxn = 2e5 + 7;
int n;
int a[maxn];
int mp[maxn], cnt[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
cin >> n;
memset(mp, 0, sizeof(mp));
memset(cnt, 0, sizeof(cnt));
_rep(i, 1, n)
{
cin >> a[i];
cnt[a[i]]++;
}
sort(a + 1, a + n + 1);
int ans = 0;
_rep(i, 1, n)
{
if (a[i] == a[i - 1])
continue;
if (a[i] != 1)
mp[a[i]] = cnt[a[i]] + cnt[1];
else
mp[1] = cnt[1];
for (int j = 2; j * j <= a[i]; j++)
{
if (a[i] % j == 0)
mp[a[i]] = max(mp[a[i]], max(mp[j], mp[a[i] / j]) + cnt[a[i]]);
}
ans = max(ans, mp[a[i]]);
}
cout << n - ans << endl;
}
}