結果

問題 No.1201 お菓子配り-4
ユーザー shauuebbitshauuebbit
提出日時 2020-08-28 23:27:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,756 ms / 4,000 ms
コード長 1,147 bytes
コンパイル時間 764 ms
コンパイル使用メモリ 70,268 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-26 14:00:52
合計ジャッジ時間 29,018 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
6,812 KB
testcase_01 AC 1,084 ms
6,940 KB
testcase_02 AC 1,562 ms
6,944 KB
testcase_03 AC 759 ms
6,944 KB
testcase_04 AC 369 ms
6,940 KB
testcase_05 AC 917 ms
6,944 KB
testcase_06 AC 140 ms
6,940 KB
testcase_07 AC 416 ms
6,940 KB
testcase_08 AC 1,159 ms
6,944 KB
testcase_09 AC 848 ms
6,940 KB
testcase_10 AC 8 ms
6,940 KB
testcase_11 AC 221 ms
6,940 KB
testcase_12 AC 1,783 ms
6,944 KB
testcase_13 AC 50 ms
6,940 KB
testcase_14 AC 26 ms
6,940 KB
testcase_15 AC 1,392 ms
6,940 KB
testcase_16 AC 425 ms
6,940 KB
testcase_17 AC 545 ms
6,944 KB
testcase_18 AC 90 ms
6,944 KB
testcase_19 AC 84 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 1 ms
6,944 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 2,253 ms
6,940 KB
testcase_31 AC 2,240 ms
6,940 KB
testcase_32 AC 2,242 ms
6,944 KB
testcase_33 AC 2,345 ms
6,940 KB
testcase_34 AC 2,246 ms
6,940 KB
testcase_35 AC 3,756 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>

const long long MOD = 1e9 + 7;

template<typename T, typename U>
T gcd(T a, U b){
  if(a < b){
    a ^= b;
    b ^= a;
    a ^= b;
  }

  T r;
  while(b){
    r = a % b;
    a = b;
    b = r;
  }

  return a;
}

template<typename T>
constexpr T inverse(T a, T m){
  T u = 0, v = 1;

  while(a){
    T t = m / a;
    m -= t * a; std::swap(a, m);
    u -= t * v; std::swap(u, v);
  }
  
  u %= MOD;
  if(u < 0) u += MOD;

  return u;
}

template<typename T>
constexpr T inverse(T a){
  return inverse(a, MOD);
}

using namespace std;

int main(){
  int N, M;
  cin >> N >> M;
  
  vector<long long> A(N), B(M);
  for(int i = 0; i < N; i++) cin >> A[i];
  for(int j = 0; j < M; j++) cin >> B[j];
  
  long long ans = 0;
  for(int i = 0; i < N; i++){
    for(int j = 0; j < M; j++){
      long long a = A[i], b = B[j];
      long long g = gcd(a, b);
      a /= g; b /= g;
      
      ans += a * B[j] % MOD * (B[j] + 1) % MOD * inverse(b) % MOD - (b - 1) * (B[j] / b) % MOD - (B[j] % b + 1) * (B[j] % b) % MOD * inverse(b) % MOD;
      ans %= MOD;
    }
  }
  
  cout << (ans + MOD) % MOD << endl;
  
  return 0;
}
0