結果

問題 No.886 Direct
ユーザー Ryuhei MoriRyuhei Mori
提出日時 2019-09-28 15:23:01
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 159 ms / 4,000 ms
コード長 1,213 bytes
コンパイル時間 1,631 ms
コンパイル使用メモリ 29,308 KB
実行使用メモリ 13,648 KB
最終ジャッジ日時 2023-07-25 14:33:54
合計ジャッジ時間 5,408 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
13,512 KB
testcase_01 AC 58 ms
13,540 KB
testcase_02 AC 58 ms
13,552 KB
testcase_03 AC 59 ms
13,600 KB
testcase_04 AC 59 ms
13,500 KB
testcase_05 AC 58 ms
13,640 KB
testcase_06 AC 59 ms
13,576 KB
testcase_07 AC 58 ms
13,508 KB
testcase_08 AC 58 ms
13,636 KB
testcase_09 AC 58 ms
13,512 KB
testcase_10 AC 59 ms
13,580 KB
testcase_11 AC 61 ms
13,600 KB
testcase_12 AC 59 ms
13,560 KB
testcase_13 AC 58 ms
13,500 KB
testcase_14 AC 60 ms
13,568 KB
testcase_15 AC 59 ms
13,512 KB
testcase_16 AC 58 ms
13,560 KB
testcase_17 AC 58 ms
13,440 KB
testcase_18 AC 57 ms
13,580 KB
testcase_19 AC 59 ms
13,540 KB
testcase_20 AC 59 ms
13,568 KB
testcase_21 AC 61 ms
13,548 KB
testcase_22 AC 59 ms
13,508 KB
testcase_23 AC 72 ms
13,548 KB
testcase_24 AC 91 ms
13,564 KB
testcase_25 AC 72 ms
13,508 KB
testcase_26 AC 69 ms
13,548 KB
testcase_27 AC 115 ms
13,576 KB
testcase_28 AC 116 ms
13,516 KB
testcase_29 AC 62 ms
13,560 KB
testcase_30 AC 63 ms
13,508 KB
testcase_31 AC 158 ms
13,584 KB
testcase_32 AC 159 ms
13,508 KB
testcase_33 AC 159 ms
13,580 KB
testcase_34 AC 159 ms
13,648 KB
testcase_35 AC 156 ms
13,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

#define N 3000000

/*
\sum_{a,b,gcd(a,b)=1} (h-a)(w-b)
f(n) := \sum_{a,b,gcd(a,b)=n} (h-a)(w-b)

F(m) := \sum_{a,b,m|gcd(a,b)} (h-a)(w-b)
= \sum_{a,m|a}(h-a) \sum_{b,m|b}(w-b)
= [h floor(h/m) - m S(floor(h/m))][w floor(w/m) - m S(floor(w/m))]

f(1) = \sum_{m} mu(m) F(m)
*/

const int mod = 1000000007;
int h, w;

long long int S(int n){
  return (long long) n * (n+1) / 2;
}

int F(int m){
  long long a = (long long) h/m*h - (long long) m * S(h/m);
  long long b = (long long) w/m*w - (long long) m * S(w/m);
  return (a % mod) * (b % mod) % mod;
}

int mu[N+1];

int main(){
  int i, j;
  long long int z;

  mu[1] = 1;
  for(i=2;i<=N;i++){
    if(!mu[i]){
      for(j=2*i;j<=N;j+=i){
        mu[j] = mu[j]?-mu[j]:-1;
      }
    }
  }
  for(i=N;i>=1;i--){
    if(!mu[i]){
      mu[i] = -1;
      if((long long) i*i > N) continue;
      for(j=i*i;j<=N;j+=i*i){
        mu[j] = 0;
      }
    }
  }

  scanf("%d%d",&h,&w);
  if(w < h){
    int tmp = h;
    h = w;
    w = tmp;
  }
  z = 0;
  for(i=1;i<=h;i++){
    z += mu[i] * F(i);
  }
  z %= mod;
  if(z < 0) z += mod;
  z += z;
  z += ((long long) (w-1) * h + (long long) (h-1) * w);
  z %= mod;
  printf("%lld\n", z);
  
  return 0;
}

0