結果

問題 No.2125 Inverse Sum
ユーザー 👑 chro_96chro_96
提出日時 2022-11-18 23:07:01
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 1,690 bytes
コンパイル時間 1,472 ms
コンパイル使用メモリ 31,228 KB
実行使用メモリ 50,824 KB
最終ジャッジ日時 2023-10-20 08:05:37
合計ジャッジ時間 3,275 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
48,848 KB
testcase_01 AC 14 ms
48,848 KB
testcase_02 AC 14 ms
48,784 KB
testcase_03 AC 15 ms
48,784 KB
testcase_04 AC 15 ms
48,848 KB
testcase_05 AC 15 ms
48,888 KB
testcase_06 AC 15 ms
48,784 KB
testcase_07 AC 15 ms
48,848 KB
testcase_08 AC 15 ms
48,848 KB
testcase_09 AC 14 ms
48,848 KB
testcase_10 AC 15 ms
48,784 KB
testcase_11 AC 15 ms
48,848 KB
testcase_12 AC 15 ms
48,848 KB
testcase_13 AC 15 ms
48,848 KB
testcase_14 AC 14 ms
48,848 KB
testcase_15 AC 14 ms
48,848 KB
testcase_16 AC 14 ms
48,848 KB
testcase_17 AC 15 ms
48,848 KB
testcase_18 AC 15 ms
48,848 KB
testcase_19 AC 15 ms
48,848 KB
testcase_20 AC 16 ms
48,848 KB
testcase_21 AC 15 ms
48,848 KB
testcase_22 AC 15 ms
48,848 KB
testcase_23 AC 15 ms
48,784 KB
testcase_24 AC 15 ms
48,848 KB
testcase_25 AC 15 ms
48,848 KB
testcase_26 AC 14 ms
48,784 KB
testcase_27 AC 55 ms
50,560 KB
testcase_28 AC 55 ms
50,824 KB
testcase_29 AC 22 ms
48,976 KB
testcase_30 AC 14 ms
48,848 KB
testcase_31 AC 55 ms
50,824 KB
testcase_32 AC 29 ms
48,848 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[3000000][2] = {};
  
  int d = 0;
  
  int div[1500] = {};
  int div_cnt = 0;
  
  res = scanf("%d", &p);
  res = scanf("%d", &q);
  
  d = gcd(p, q);
  p /= d;
  q /= d;
  
  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++;
      }
    }
  }
  
  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 ((q/d2)%d1 == 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