题解CF757B

题目
题意:在 \(s\) 数组中找出尽可能多的数使得他们的最大公约数 \(>1\)
既然最大公约数 \(>1\)\(s\) 数组的值域是 \(1 \le s_i \le 10^5\),所以可以尝试枚举我所要求的最大公约数。
这样的话我要 \(O(n\sqrt{n}))\) 做出每个 \(s_i\) 的因子。
然后就搞定了。
可以在枚举最大公约数的时候只做素数,但优化不大。(\(O(n\sqrt n + n) \rightarrow O(n\sqrt n+\sqrt n)\)

#include<stdio.h>
#include<math.h>
#define reg register
#define ri reg int
#define rep(i, x, y) for(ri i = x; i <= y; ++i)
#define nrep(i, x, y) for(ri i = x; i >= y; --i)
#define DEBUG 1
#define ll long long
#define il inline
#define max(i, j) (i) > (j) ? (i) : (j)
#define min(i, j) (i) < (j) ? (i) : (j)
#define read(i) io.READ(i)
#define print(i) io.WRITE(i)
#define push(i) io.PUSH(i)
struct IO {
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
  char buf[MAXSIZE], *p1, *p2;
  char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
  IO() : p1(buf), p2(buf), pp(pbuf) {}
  ~IO() {
    fwrite(pbuf, 1, pp - pbuf, stdout);
  }
#endif
  inline char gc() {
#if DEBUG
    return getchar();
#endif
    if(p1 == p2)
      p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
    return p1 == p2 ? ' ' : *p1++;
  }
  inline bool blank(char ch) {
    return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
  }
  template <class T>
  inline void READ(T &x) {
    register double tmp = 1;
    register bool sign = 0;
    x = 0;
    register char ch = gc();
    for(; !isdigit(ch); ch = gc())
      if(ch == '-') sign = 1;
    for(; isdigit(ch); ch = gc())
      x = x * 10 + (ch - '0');
    if(ch == '.')
      for(ch = gc(); isdigit(ch); ch = gc())
        tmp /= 10.0, x += tmp * (ch - '0');
    if(sign) x = -x;
  }
  inline void READ(char *s) {
    register char ch = gc();
    for(; blank(ch); ch = gc());
    for(; !blank(ch); ch = gc())
      *s++ = ch;
    *s = 0;
  }
  inline void READ(char &c) {
    for(c = gc(); blank(c); c = gc());
  }
  inline void PUSH(const char &c) {
#if DEBUG
    putchar(c);
#else
    if(pp - pbuf == MAXSIZE) {
      fwrite(pbuf, 1, MAXSIZE, stdout);
      pp = pbuf;
    }
    *pp++ = c;
#endif
  }
  template <class T>
  inline void WRITE(T x) {
    if(x < 0) {
      x = -x;
      PUSH('-');
    }
    static T sta[35];
    T top = 0;
    do {
      sta[top++] = x % 10;
      x /= 10;
    } while(x);
    while(top)
      PUSH(sta[--top] + '0');
  }
  template <class T>
  inline void WRITE(T x, char lastChar) {
    WRITE(x);
    PUSH(lastChar);
  }
} io;
int s[100010], n, a[100010];
int g_P(int n) {
  rep(i, 1, n) {
    ri l = sqrt(a[i]);
    rep(j, 1, l) {
	    if(a[i] % j == 0) if(a[i] / j != j) ++s[j], ++s[a[i] / j];
	      else ++s[j];
		}
	}
}
int main() {
  ri ans = 0;
  read(n);
  rep(i, 1, n) read(a[i]);
  g_P(n);
  rep(i, 2, 100000) ans = max(ans, s[i]);
  print(ans == 0 ? 1 : ans);
  return 0;
} 
posted @ 2021-03-27 08:15  1358id  阅读(72)  评论(0编辑  收藏  举报