Codeforces Round #643 (Div. 2)

题目传送门

还是视频题解

我在这简单说一下D和F吧。
关于D题的证明:
我们首先如下构造:
\(1\ 1\ 1\cdots s-(n-1)\)
如果\(n-1<s-(n-1)-1\)显然存在一种构造方式使得另一个人gg,我们接下来就考虑\(n-1\geq s-(n-1)-1\)\(2n>s\)的情况。

  • 现在有\(s<2n\),并且假设我们可以构造出一种合法的序列\(a\)和一个\(k\),使得序列找不到一段区间和为\(k\)或者\(s-k\)。我们现在考虑将\(a\)进行不断地拼接,那么上述条件就转化为不存在一段区间和为\(k\)
  • 我们截取\(2k\)段,因\(a_i>0\),所以我们容易发现有\(2kn\)个前缀,并且前缀和值各不相同,即有\(2kn\)个不同地值。
  • 显然和的最大值为\(2ks\),我们将\(1...2ks\)分组:\([1,k+1],[2,k+2],...,[2ks-k,2ks]\),由于限制条件,每组至多选择一个作为前缀和的值,所以至多\(ks\)个值。
  • 故有\(2kn\leq ks\)\(2n\leq s\),与我们的前提不符,矛盾。

所以就证明了当\(s<2n\)时无解的情况。
感觉这证明方法还是挺巧妙的吧。。貌似很少见到这种类似的证明?

\(F\)题也是一个很巧妙的题,需要将题目所给出的条件利用到最大化。

  • \(\frac{d}{2}\leq ans\leq 2d\),我们可以发现至多可以不用知道两个次数为\(1\)的质因子或者一个次数至多为\(2\)的质因子。
  • 因此我们可以将范围减小到\(700\),我们假设求出了\(X\)中质因子不超过\(700\)的部分\(X_1\),若\(X_1\geq 3\),那么\(3\cdot 700\cdot 700\cdot 700>10^9\),显然此时满足上面可以不用管的情况,我们直接输出\(2X_1\);否则我们单独考虑一下,用上\(d-7\leq ans\leq d+7\)
  • 但是此时询问次数要超限,我们注意到询问\(Q\leq 10^{18}\),所以对一个质因子\(p_i\leq 700\)而言,我们可以两个质因子乘在一起询问,因为需要\(9\)个质数连乘才刚好到\(10^9\),所以我们这里需要\(5\)补。
  • 但是通过连乘找\(700\)的所有素数,貌似需要\(18\)次,所以我们这里还要再次进行缩小,大约缩到\(600\)多就行了。

所以这个题也差不多做完了,就充分利用各种条件就行,神奇。


代码如下:

A. Sequence with Digits
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/16 19:38:21
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5;
 
void run() {
    ll a, k;
    cin >> a >> k;
    --k;
    while (k--) {
        ll x = a;
        int Min = 10, Max = -1;
        while (x) {
            Min = min(Min, int(x % 10));
            Max = max(Max, int(x % 10));
            x /= 10;
        }
        if (Min == 0) break;
        a += Min * Max;
    }
    cout << a << '\n';
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    int T; cin >> T; while(T--)
    run();
    return 0;
}
B. Young Explorers
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/16 19:44:50
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 2e5 + 5;
 
int n;
int a[N];
 
void run() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    int ans = 0;
    int Min = -1, cnt = 0;
    for (int i = 1; i <= n; i++) {
        if (Min == -1) {
            cnt = 0;
        }
        Min = max(Min, a[i]);
        if (++cnt == Min) {
            Min = -1;
            ++ans;
        }
    }
    cout << ans << '\n';
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    int T; cin >> T; while(T--)
    run();
    return 0;
}
C. Count Triangles
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/16 23:03:40
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e6 + 5;
 
int cnt[N];
 
void run() {
    int A, B, C, D; cin >> A >> B >> C >> D;
    ++cnt[A + B], --cnt[B + B + 1];
    --cnt[A + C + 1], ++cnt[B + C + 2];
    for (int i = 1; i < N; i++) {
        cnt[i] += cnt[i - 1];
    }
    for (int i = 1; i < N; i++) {
        cnt[i] += cnt[i - 1];
    }
    ll ans = 0;
    for (int i = C + 1; i < N; i++) {
        int val = min(i - 1, D) - C + 1;
        ans += 1ll * val * cnt[i];
    }
    cout << ans << '\n';
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}
D. Game With Array
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/16 20:24:54
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5;
 
void run() {
    int n, s; cin >> n >> s;
    if (n == s) {
        cout << "NO" << '\n';
        return;
    }
    int t = s - (n - 1);
    if (n - 1 >= t - 1 || s - n >= t) {
        cout << "NO" << '\n';
        return;   
    }
    cout << "YES" << '\n';
    for (int i = 1; i < n; i++) cout << 1 << ' ';
    cout << t << '\n';
    cout << n << '\n';
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}
E. Restorer Distance
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/16 22:21:11
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5;
 
int n, A, R, M;
int h[N];
 
void run() {
    cin >> n >> A >> R >> M;
    M = min(M, A + R);
    for (int i = 1; i <= n; i++) {
        cin >> h[i];
    }
    auto f = [&] (int x) {
        ll cnt_a = 0, cnt_b = 0;
        for (int i = 1; i <= n; i++) {
            if (h[i] < x) cnt_a += x - h[i];
            if (h[i] > x) cnt_b += h[i] - x;   
        }
        ll cnt_c = min(cnt_a, cnt_b);
        cnt_a -= cnt_c;
        cnt_b -= cnt_c;
        return cnt_a * A + cnt_b * R + cnt_c * M;
    };
    int l = 0, r = INF, lmid, rmid;
    while (l < r) {
        lmid = l + ((r - l) / 3);   
        rmid = r - ((r - l) / 3);
        if (f(lmid) < f(rmid)) {
            r = rmid - 1;
        } else {
            l = lmid + 1;
        }
    }
    cout << f(l) << '\n';
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}
F. Guess Divisors Count
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/17 11:13:54
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e3 + 5;
const int B = 630;
const ll MAX = 1e18;
int primes[N], tot;
bool vis[N];
void init() {
    for (int i = 2; i < N; i++) {
        if (!vis[i]) {
            primes[++tot] = i;
            vis[i] = true;
            for (ll j = 1ll * i * i; j < N; j += i) {
                vis[j] = true;
            }
        }
    }
}
 
ll query(ll x) {
    cout << "? " << x << endl;
    ll t; cin >> t;
    return t;   
}
void answer(int x) {
    cout << "! " << x << endl;
}
 
void run() {
    int cnt = 0;
    vector <int> d;
    for (int i = 0, j = 1; i < 17; i++) {
        ll x = 1;
        while (x <= MAX / primes[j]) {
            x *= primes[j];
            ++j;
        }
        int t = query(x);
        for (int k = 1; k <= j; k++) {
            if (t % primes[k] == 0) {
                d.push_back(primes[k]);
            }
        }       
    }
    
    auto gao = [&](int x) {
        int res = 1;
        while (1ll * res * x <= 1e9) {
            res *= x;
        }
        return res;
    };
    
	auto calc = [&](int& t, int x) {
		int cnt = 0;
		while (t % x == 0) {
			t /= x;
			++cnt;
		}
		return cnt;
	};
	
    int x1 = 1, c = 1;
    for (int i = 0; i < sz(d); i += 2) {
        ll now = 1;
    	now *= gao(d[i]);
        int j = i + 1;
        if (j < sz(d)) now *= gao(d[j]);
        int t = query(now);
        x1 *= t;
        c *= (calc(t, d[i]) + 1);
        if (j < sz(d)) {
            c *= (calc(t, d[j]) + 1);   
        }
	}
    if (x1 >= 4) answer(c << 1);
    if (x1 == 1) answer(8);
    if (x1 == 2 || x1 == 3) answer(9);
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    init();
    int T; cin >> T; while(T--)
    run();
    return 0;
}
posted @ 2020-05-18 09:37  heyuhhh  阅读(445)  评论(5编辑  收藏  举报