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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM