結果

問題 No.2125 Inverse Sum
ユーザー 👑 chro_96chro_96
提出日時 2022-11-19 11:48:31
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 2,206 bytes
コンパイル時間 790 ms
コンパイル使用メモリ 32,220 KB
実行使用メモリ 33,872 KB
最終ジャッジ日時 2023-10-20 17:16:29
合計ジャッジ時間 2,569 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
33,224 KB
testcase_01 AC 10 ms
33,224 KB
testcase_02 AC 10 ms
33,160 KB
testcase_03 AC 10 ms
33,160 KB
testcase_04 AC 10 ms
33,224 KB
testcase_05 AC 11 ms
33,224 KB
testcase_06 AC 10 ms
33,160 KB
testcase_07 AC 10 ms
33,224 KB
testcase_08 AC 10 ms
33,224 KB
testcase_09 AC 10 ms
33,224 KB
testcase_10 AC 10 ms
33,160 KB
testcase_11 AC 10 ms
33,224 KB
testcase_12 AC 10 ms
33,224 KB
testcase_13 AC 9 ms
33,224 KB
testcase_14 AC 9 ms
33,224 KB
testcase_15 AC 10 ms
33,224 KB
testcase_16 AC 10 ms
33,224 KB
testcase_17 AC 10 ms
33,224 KB
testcase_18 AC 9 ms
33,224 KB
testcase_19 AC 10 ms
33,224 KB
testcase_20 AC 10 ms
33,224 KB
testcase_21 AC 9 ms
33,224 KB
testcase_22 AC 10 ms
33,224 KB
testcase_23 AC 10 ms
33,160 KB
testcase_24 AC 10 ms
33,224 KB
testcase_25 AC 11 ms
33,224 KB
testcase_26 AC 10 ms
33,160 KB
testcase_27 AC 28 ms
33,872 KB
testcase_28 AC 25 ms
33,608 KB
testcase_29 AC 17 ms
33,344 KB
testcase_30 AC 10 ms
33,224 KB
testcase_31 AC 24 ms
33,608 KB
testcase_32 AC 12 ms
33,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>

int cmp_ll (const void *ap, const void *bp) {
  long long a = *(long long *)ap;
  long long b = *(long long *)bp;
  
  if (a < b) {
    return -1;
  }
  
  if (a > b) {
    return 1;
  }
  
  return 0;
}

int gcd (int a, int b) {
  if (a <= 0 || b <= 0) {
    return a+b;
  }
  
  if (a%b == 0) {
    return b;
  }
  
  return gcd(b, a%b);
}

int main () {
  int p = 0;
  int q = 0;
  
  int res = 0;
  
  int ans_cnt = 0;
  int tmp_cnt = 0;
  long long ans[2000000][2] = {};
  
  int d = 0;
  
  int div[1500] = {};
  int div_cnt = 0;
  
  int b[1500] = {};
  
  int tmp = 0;
  int primes[32] = {};
  int primes_cnt = 0;
  
  res = scanf("%d", &p);
  res = scanf("%d", &q);
  
  d = gcd(p, q);
  p /= d;
  q /= d;
  
  tmp = q;
  for (int i = 1; i*i <= q; i++) {
    if (q%i == 0) {
      div[div_cnt] = i;
      div_cnt++;
      if (q/i != i) {
        div[div_cnt] = q/i;
        div_cnt++;
      }
    }
  }
  
  tmp = q;
  for (int i = 2; i*i <= q; i++) {
    if (tmp%i == 0) {
      primes[primes_cnt] = i;
      primes_cnt++;
      while (tmp%i == 0) {
        tmp /= i;
      }
    }
  }
  
  if (tmp > 1) {
    primes[primes_cnt] = tmp;
    primes_cnt++;
  }
  
  for (int i = 0; i < div_cnt; i++) {
    for (int j = 0; j < primes_cnt; j++) {
      if (div[i]%primes[j] == 0) {
        b[i] |= (1<<j);
      }
    }
  }
  
  for (int i = 0; i < div_cnt; i++) {
    int d1 = div[i];
    for (int j = 0; j < div_cnt; j++) {
      int d2 = div[j];
      if ((b[i]&b[j]) == 0 && (d1+d2)%p == 0) {
        long long mul = (long long) (q/(d1*d2));
        mul *= (long long) ((d1+d2)/p);
        ans[tmp_cnt][0] = mul*((long long)d1);
        ans[tmp_cnt][1] = mul*((long long)d2);
        tmp_cnt++;
      }
    }
  }
  
  if (tmp_cnt <= 0) {
    printf("0\n");
    return 0;
  }
  
  qsort(ans, tmp_cnt, sizeof(long long)*2, cmp_ll);
  
  ans_cnt = 1;
  for (int i = 1; i < tmp_cnt; i++) {
    if (ans[i-1][0] != ans[i][0]) {
      ans[ans_cnt][0] = ans[i][0];
      ans[ans_cnt][1] = ans[i][1];
      ans_cnt++;
    }
  }
  
  printf("%d\n", ans_cnt);
  for (int i = 0; i < ans_cnt; i++) {
    printf("%lld %lld\n", ans[i][0], ans[i][1]);
  }
  
  return 0;
}
0