• CF1305 F. Kuroni and the Punishment [随机化]


    大致题意:每次操作可以使得序列某个数-1/+1,问最少多少次操作使得全局gcd>1

    死也想不到随机化。。

    我们考虑到 (gcd == 2) 的情况,显然答案的上界是 (n)
    我们考虑答案是不可能超过 (n) 的,那么至少 (frac{n}{2}) 是 0 或 1 。
    然后直接随机 (x)(x - 1) , (x + 1) 就可以了。
    每次选中的概率是 (frac{1}{2})
    所以你选 (m) 个数,那么正确率就是 (1 - frac{1}{2^m})

    // powered by c++11
    // by Isaunoya
    #include <bits/stdc++.h>
    #define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
    #define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
    using namespace std;
    using db = double;
    using ll = long long;
    using uint = unsigned int;
    #define Tp template
    using pii = pair<int, int>;
    #define fir first
    #define sec second
    Tp<class T> void cmax(T& x, const T& y) {if (x < y) x = y;} Tp<class T> void cmin(T& x, const T& y) {if (x > y) x = y;}
    #define all(v) v.begin(), v.end()
    #define sz(v) ((int)v.size())
    #define pb emplace_back
    Tp<class T> void sort(vector<T>& v) { sort(all(v)); } Tp<class T> void reverse(vector<T>& v) { reverse(all(v)); }
    Tp<class T> void unique(vector<T>& v) { sort(all(v)), v.erase(unique(all(v)), v.end()); }
    const int SZ = 1 << 23 | 233;
    struct FILEIN { char qwq[SZ], *S = qwq, *T = qwq, ch;
    #ifdef __WIN64
    #define GETC getchar
    #else
      char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
    #endif
      FILEIN& operator>>(char& c) {while (isspace(c = GETC()));return *this;}
      FILEIN& operator>>(string& s) {while (isspace(ch = GETC())); s = ch;while (!isspace(ch = GETC())) s += ch;return *this;}
      Tp<class T> void read(T& x) { bool sign = 0;while ((ch = GETC()) < 48) sign ^= (ch == 45); x = (ch ^ 48);
        while ((ch = GETC()) > 47) x = (x << 1) + (x << 3) + (ch ^ 48); x = sign ? -x : x;
      }FILEIN& operator>>(int& x) { return read(x), *this; } FILEIN& operator>>(ll& x) { return read(x), *this; }
    } in;
    struct FILEOUT {const static int LIMIT = 1 << 22 ;char quq[SZ], ST[233];int sz, O;
      ~FILEOUT() { flush() ; }void flush() {fwrite(quq, 1, O, stdout); fflush(stdout);O = 0;}
      FILEOUT& operator<<(char c) {return quq[O++] = c, *this;}
      FILEOUT& operator<<(string str) {if (O > LIMIT) flush();for (char c : str) quq[O++] = c;return *this;}
      Tp<class T> void write(T x) {if (O > LIMIT) flush();if (x < 0) {quq[O++] = 45;x = -x;}
    		do {ST[++sz] = x % 10 ^ 48;x /= 10;} while (x);while (sz) quq[O++] = ST[sz--];
      }FILEOUT& operator<<(int x) { return write(x), *this; } FILEOUT& operator<<(ll x) { return write(x), *this; }
    } out;
    #define int long long
    set < int > s ;
    signed main() {
      // code begin.
    	auto get = [&](int x) {
    		for(int i = 2 ; i <= sqrt(x) ; i ++) {
    			if(! (x % i)) {
    				s.insert(i) ;
    				while(! (x % i)) x /= i ;
    			}
    		}
    		if(x > 1) s.insert(x) ;
    	}	;
    	int n ;
    	in >> n ;
    	vector < int > a(n , 0) ;
    	for(int i = 0 ; i < n ; i ++) in >> a[i] ;
    	sort(a.begin() , a.end()) ;
    	int m = min(n , 30ll) ;
    	random_shuffle(a.begin() , a.end()) ;
    	for(int i = 0 ; i < m ; i ++) get(a[i]) ;
    	random_shuffle(a.begin() , a.end()) ;
    	for(int i = 0 ; i < m ; i ++) get(a[i] + 1) ;
    	random_shuffle(a.begin() , a.end()) ;
    	for(int i = 0 ; i < m ; i ++) get(a[i] - 1) ;
    	auto solve = [&](int x) {
    		int cnt = 0 ;
    		for(int i = 0 ; i < n ; i ++) {
    			if(a[i] < x)
    				cnt += x - a[i] ;
    			else
    				cnt += min(a[i] % x , x - a[i] % x) ;
    		}
    		return cnt ;
    	} ;
    	int ans = n ;
    	while(s.size()) {
    		cmin(ans , solve(* s.begin())) ;
    		s.erase(s.begin()) ;
    	}
    	out << ans << '
    ' ;
    	return 0;
      // code end.
    }
    
  • 相关阅读:
    欧拉函数(线性筛)(超好Dong)
    欧拉函数(线性筛)(超好Dong)
    线性素数筛(欧拉筛)(超级好的MuBan)
    线性素数筛(欧拉筛)(超级好的MuBan)
    Fire Game (FZU 2150)(BFS)
    Fire Game (FZU 2150)(BFS)
    Fantasy of a Summation (LightOJ
    Java——接口
    Java——异常处理
    Java——数组
  • 原文地址:https://www.cnblogs.com/Isaunoya/p/12411474.html
Copyright © 2020-2023  润新知