其他分享
首页 > 其他分享> > Codeforces Round #697 (Div. 3)

Codeforces Round #697 (Div. 3)

作者:互联网

A. Odd Divisor

题意

询问一个数 \(n\) 是否包含除 1 外的质因子

思路

不停除 2

/*
 * @Author: zhl
 * @LastEditTime: 2021-01-25 23:02:11
 */

/* Author: wxhgkk
 * Time: 2021-01-25 23:00:02
**/

#include<bits/stdc++.h>
//#define int long long
using namespace std;

typedef long long ll;
const int N = 3e5 + 100;
int T, k, m, A[N];
ll n;
string s;

void init() {
    cin >> n;
    
}
void solve() {
    while(n > 1){
        if (n & 1ll) {
            cout << "YES" << endl;
            return;
        }
        n >>= 1ll;
    }
    cout << "NO" << endl;
}
signed main() {
	 //ios::sync_with_stdio(0);cin.tie(0);
    cin >> T;
    while (T--) {
        init();
        solve();
    }
}

B. New Year's Number

题意

给 \(n\) ,判断 \(n\) 能否表示为若干个 2020 与 2021 之和

思路

暴力枚举

/*
 * @Author: zhl
 * @LastEditTime: 2021-01-25 23:05:20
 */

/* Author: wxhgkk
 * Time: 2021-01-25 23:00:02
**/

#include<bits/stdc++.h>
//#define int long long
using namespace std;

typedef long long ll;
const int N = 3e5 + 100;
int T, n, k, m, A[N];
string s;



void init() {
    cin >> n;
    
}
void solve() {
    for(int i = 0;i <= n;i += 2020){
        if((n - i) % 2021 == 0){
            cout << "YES" << endl;
            return;
        }
    }
    cout << "NO" << endl;
    
}
signed main() {
    //ios::sync_with_stdio(0);cin.tie(0);
    cin >> T;
    while (T--) {
        init();
        solve();
    }
}


C. Ball in Berland

题意

给一个二分图,选两条不同的边,问有多少种选择使得选中的四个点各不相同

思路

\(C_k^2 - \sum_v{C_{cnt_v} ^2}\)

总方案减去非法方案,枚举重合的点

/*
 * @Author: zhl
 * @LastEditTime: 2021-01-25 23:13:11
 */

/* Author: wxhgkk
 * Time: 2021-01-25 23:00:02
**/

#include<bits/stdc++.h>
//#define int long long
using namespace std;

typedef long long ll;
const int N = 3e5 + 100;
int T;
int A[N], B[N];
int a, b, k;

string s;

void init() {
    cin >> a >> b >> k;
    for (int i = 0;i <= a;i++)A[i] = 0;
    for (int i = 0;i <= b;i++)B[i] = 0;
    for(int i = 1;i <= k;i++){
        int x;cin >> x;A[x]++;
    }
    for(int i = 1;i <= k;i++){
        int x;cin >> x;B[x]++;
    }
}
void solve() {
    ll ans = 1ll * k * (k - 1) / 2ll;
    for(int i = 1;i <= a;i++){
        ans -= 1ll * A[i] * (A[i] - 1ll) / 2ll;
    }
    for(int i = 1;i <= b;i++){
        ans -= 1ll * B[i] * (B[i] - 1ll) / 2ll;
    }
    cout << ans << endl;
}
signed main() {
    //ios::sync_with_stdio(0);cin.tie(0);
    cin >> T;
    while (T--) {
        init();
        solve();
    }
}


D. Cleaning the Phone

题意

思路

枚举 2 的次数,二分 1 的次数

/*
 * @Author: zhl
 * @LastEditTime: 2021-01-26 00:56:27
 */
#include<bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10;
typedef long long ll;

int T, n;
ll m;
ll A[N], cnta;
ll B[N], cntb;
ll tmp[N];
int main() {
	cin >> T;
	while (T--) {
		cin >> n >> m;
		for (int i = 1; i <= n; i++)cin >> tmp[i];
		cnta = cntb = 0;
		for (int i = 1; i <= n; i++) {
			int x; cin >> x;
			if (x == 1)A[++cnta] = tmp[i];
			else B[++cntb] = tmp[i];
		}
		sort(A + 1, A + 1 + cnta, greater<ll>());
		sort(B + 1, B + 1 + cntb, greater<ll>());
		for (int i = 1; i <= cnta; i++)A[i] += A[i - 1];
		for (int i = 1; i <= cntb; i++)B[i] += B[i - 1];

		if (A[cnta] + B[cntb] < m)puts("-1");
		else {
			ll ans = cnta + 2 * cntb;
			for (int i = 0; i <= cntb; i++) {
				ll now = B[i];
				if (A[cnta] < m - now)continue;
				int pos = lower_bound(A, A + 1 + cnta, m - now) - A;
				ans = min(ans, 2ll * i + pos);
			}
			printf("%lld\n", ans);
		}
	}
}

E. Advertising Agency

思路

很显然从大到小贪心,看最小的数有多少个就可以

/*
 * @Author: zhl
 * @LastEditTime: 2021-01-25 23:41:59
 */

 /* Author: wxhgkk
  * Time: 2021-01-25 23:00:02
 **/

#include<bits/stdc++.h>
 //#define int long long
using namespace std;

typedef long long ll;
const int N = 3e5 + 100, mod = 1e9 + 7;
int T, n, k, m, A[N];
string s;

ll fac[N], invfac[N], inv[N];

void _init() {
    fac[0] = invfac[0] = 1;
    fac[1] = invfac[1] = 1;
    inv[1] = 1;
    for (int i = 2;i < N;i++) {
        fac[i] = fac[i - 1] * i % mod;
        inv[i] = (mod - mod / i) * inv[mod % i] % mod;
        invfac[i] = invfac[i - 1] * inv[i] % mod;
    }
}

int C(int n, int m) {
    return 1ll * fac[n] * invfac[n - m] % mod * invfac[m] % mod;
}
map<int, int>cnt;
void init() {
    cin >> n >> k;cnt.clear();
    for (int i = 1;i <= n;i++)cin >> A[i], cnt[A[i]]++;
}
void solve() {
    sort(A + 1, A + 1 + n, greater<int>());
    int now = 0;int pre = -1;
    for (int i = 1;i <= k;i++) {
        if (A[i] == pre) {
            now++;
        }
        else {
            now = 1;
        }

        pre = A[i];
    }
    cout << C(cnt[A[k]], now) << endl;
}
signed main() {
    //ios::sync_with_stdio(0);cin.tie(0);
    cin >> T;_init();
    while (T--) {
        init();
        solve();
    }
}


F. Unusual Matrix

思路

A, B异或一下,其实就是看这个矩阵能否能由初始全 0 的矩阵得到

很显然对于每一列或每一行,只有两种状态,判断就可以了

/*
 * @Author: zhl
 * @LastEditTime: 2021-01-26 00:16:28
 */

 /* Author: wxhgkk
  * Time: 2021-01-25 23:00:02
 **/

#include<bits/stdc++.h>
 //#define int long long
using namespace std;

typedef long long ll;
const int N = 1e3 + 100;
int T, n;
int A[N][N], B[N][N];
string s;

void init() {
	cin >> n;
	for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)scanf("%01d", &A[i][j]);
	//for (int i = 1;i <= 2 * n;i++)fa[i] = i;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			scanf("%01d", &B[i][j]);; B[i][j] ^= A[i][j];
		}
	}
}
void solve() {
	for (int i = 1; i <= n; i++) {
		int ans = B[i][1] ^ B[1][1];
		for (int j = 1; j <= n; j++) {
			if (B[i][j] ^ B[1][j] != ans) {
				cout << "NO" << endl;
				return;
			}
		}
	}
	cout << "YES" << endl;
}
signed main() {
	//ios::sync_with_stdio(0);cin.tie(0);
	cin >> T;
	while (T--) {
		init();
		solve();
	}
}


G. Strange Beauty

思路

发现对于一个序列,只要当前项 f[i] 是前一项 f[i-1] 的倍数就满足题意

所以直接对所有因数转移就可以,因为 \(A[i] \le 2e5\)

处理出 2e5 的所有因数就可以

\(n(\dfrac 1 1 + \dfrac 1 2 + ... + \dfrac 1 n)\) 括号里是调和级数,是 \(logn\) 级别,

复杂度 \(O(Tnlogn)\)

/*
 * @Author: zhl
 * @LastEditTime: 2021-01-26 00:45:24
 */

 /* Author: wxhgkk
  * Time: 2021-01-25 23:00:02
 **/

#include<bits/stdc++.h>
 //#define int long long
using namespace std;

typedef long long ll;
const int N = 3e5 + 100;
int T, n, k, m, A[N];
int f[N];
string s;

vector<int>G[N];
void __init() {
	for (int i = 1; i <= 200000; i++) {
		for (int j = 1; j * i <= 200000; j++) {
			G[j * i].push_back(i);
		}
	}
}
void init() {
	cin >> n;
	for (int i = 1; i <= n; i++)cin >> A[i];
	sort(A + 1, A + 1 + n);
}
void solve() {
	memset(f, 0, sizeof f);
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		int val = A[i];
		int res = 0;
		for (int v : G[val])res = max(res, f[v] + 1);
		ans = max(ans, f[val] = res);
	}
	cout << n - ans << endl;
}
signed main() {
	ios::sync_with_stdio(0);cin.tie(0);
	cin >> T; __init();
	while (T--) {
		init();
		solve();
	}
}


标签:697,Author,int,ll,Codeforces,long,init,2021,Div
来源: https://www.cnblogs.com/sduwh/p/14328284.html