結果

問題 No.2767 Add to Divide
ユーザー tnakao0123
提出日時 2024-06-01 12:08:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 723 bytes
コンパイル時間 466 ms
コンパイル使用メモリ 40,320 KB
最終ジャッジ日時 2025-02-21 19:00:53
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 16
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:25:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   25 |   scanf("%d", &tn);
      |   ~~~~~^~~~~~~~~~~
main.cpp:29:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   29 |     scanf("%d%d", &a, &b);
      |     ~~~~~^~~~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2767.cc:  No.2767 Add to Divide - yukicoder
 */

#include<cstdio>
#include<algorithm>
 
using namespace std;

/* constant */

const int INF = 1 << 30;

/* typedef */

/* global variables */

/* subroutines */

/* main */

int main() {
  int tn;
  scanf("%d", &tn);

  while (tn--) {
    int a, b;
    scanf("%d%d", &a, &b);

    if (b % a == 0) puts("0");
    else {
      // b+x=k(a+x) [k>=2,x>0] -> b-a=k(a+x)-a-x=(k-1)(a+x)
      int minx = INF, d = b - a;
      for (int p = 1; p * p <= d; p++)
	if (d % p == 0) {
	  int q = d / p;
	  if (p >= a) minx = min(minx, p - a);
	  if (q >= a) minx = min(minx, q - a);
	}
      printf("%d\n", (minx < INF) ? minx : -1);
    }
  }

  return 0;
}
0