結果

問題 No.854 公平なりんご分配
ユーザー tnakao0123
提出日時 2019-08-03 13:39:10
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 377 ms / 3,153 ms
コード長 2,485 bytes
コンパイル時間 663 ms
コンパイル使用メモリ 85,452 KB
実行使用メモリ 124,928 KB
最終ジャッジ日時 2024-07-05 17:37:47
合計ジャッジ時間 10,020 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 92
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:94:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   94 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
main.cpp:101:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  101 |     scanf("%d", &ai);
      |     ~~~~~^~~~~~~~~~~
main.cpp:115:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  115 |   scanf("%d", &q);
      |   ~~~~~^~~~~~~~~~
main.cpp:119:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  119 |     scanf("%d%d%d", &p, &l, &r);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 854.cc:  No.854 公平なりんご分配 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_E2 = 1 << 18; // 262144
const int MAX_P = 2000;
const int PN = 310;

/* typedef */

typedef int vec[PN];

/* global variables */

bool primes[MAX_P + 1];
vec pnums, afss[MAX_N + 1];
int zss[MAX_N + 1];

/* subroutines */

int gen_primes(int maxp, vec pnums) {
  memset(primes, true, sizeof(primes));
  primes[0] = primes[1] = false;

  int p, pn = 0;
  for (p = 2; p * p <= maxp; p++)
    if (primes[p]) {
      pnums[pn++] = p;
      for (int q = p * p; q <= maxp; q += p) primes[q] = false;
    }
  for (; p <= maxp; p++)
    if (primes[p]) pnums[pn++] = p;
  return pn;
}

bool prime_decomp(int n, int pn, vec pnums, vec fs) {
  for (int i = 0; i < pn; i++) {
    int &pi = pnums[i];
    fs[i] = 0;
    while (n % pi == 0) n /= pi, fs[i]++;
  }
  return (n == 1);
}

void copyvec(int n, vec c, vec a) { copy(a, a + n, c); }

void addvec(int n, vec c, vec a, vec b) {
  for (int i = 0; i < n; i++) c[i] = a[i] + b[i];
}

void subvec(int n, vec c, vec a, vec b) {
  for (int i = 0; i < n; i++) c[i] = a[i] - b[i];
}

bool cmpvec(int n, vec a, vec b) {
  bool ok = true;
  for (int i = 0; ok && i < n; i++)
    ok = (a[i] >= b[i]);
  return ok;
}

/* main */

int main() {
  int pn = gen_primes(MAX_P, pnums);
  //printf("pn=%d\n", pn); return 0;
  
  int n;
  scanf("%d", &n);

  memset(afss[0], 0, sizeof(vec));
  zss[0] = 0;
  
  for (int i = 0; i < n; i++) {
    int ai;
    scanf("%d", &ai);
    if (ai == 0) {
      copyvec(pn, afss[i + 1], afss[i]);
      zss[i + 1] = zss[i] + 1;
    }
    else {
      vec fs;
      prime_decomp(ai, pn, pnums, fs);
      addvec(pn, afss[i + 1], afss[i], fs);
      zss[i + 1] = zss[i];
    }
  }

  int q;
  scanf("%d", &q);

  while (q--) {
    int p, l, r;
    scanf("%d%d%d", &p, &l, &r);
    l--;

    if (zss[r] > zss[l]) puts("Yes");
    else {
      vec fs, pfs;
      if (! prime_decomp(p, pn, pnums, pfs))
	puts("NO");
      else {
	subvec(pn, fs, afss[r], afss[l]);
	if (cmpvec(pn, fs, pfs)) puts("Yes");
	else puts("NO");
      }
    }
  }
  return 0;
}
0