結果

問題 No.2127 Mod, Sum, Sum, Mod
ユーザー 👑 p-adicp-adic
提出日時 2022-10-28 16:08:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,269 ms / 2,000 ms
コード長 4,831 bytes
コンパイル時間 1,976 ms
コンパイル使用メモリ 201,220 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-05 20:19:14
合計ジャッジ時間 4,801 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 133 ms
5,376 KB
testcase_24 AC 505 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 1,269 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 4 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 愚直解との折衷で検算(2つ前の提出はミス、1つ前の提出はO(min(M,N))じゃなくてO(M))
#include<bits/stdc++.h>
using namespace std;

using ll = long long;

#define TYPE_OF( VAR ) remove_const<remove_reference<decltype( VAR )>::type >::type
#define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ) 
#define CIN( LL , A ) LL A; cin >> A 
#define ASSERT( A , MIN , MAX ) assert( MIN <= A && A <= MAX ) 
#define CIN_ASSERT( A , MIN , MAX ) CIN( TYPE_OF( MAX ) , A ); ASSERT( A , MIN , MAX ) 
#define FOREQ( VAR , INITIAL , FINAL ) for( TYPE_OF( FINAL ) VAR = INITIAL ; VAR <= FINAL ; VAR ++ )  
#define QUIT return 0 
#define RETURN( ANSWER ) cout << ( ANSWER ) << "\n"; QUIT 

int main()
{
  UNTIE;
  constexpr const ll bound = 1000000000;
  CIN_ASSERT( N , 1 , bound );
  CIN_ASSERT( M , 1 , bound );
  // sum( ll i = 1 ; i <= N ; i++ ) sum( ll j = 1 ; j <= M ; j++ ){ i % j }
  // = sum( ll j = 1 ; j <= M ; j++ ) sum( ll i = 1 ; i <= N ; i++ ){ i % j }
  // = sum( ll j = 1 ; j <= M ; j++ ){ ( N / j ) * ( ( j - 1 ) * j ) / 2 + ( N % j ) * ( N % j + 1 ) / 2 }
  constexpr const ll P = 998244353;
  if( M < 100000000 ){
    ll answer0 = 0;
    ll answer1 = 0;
    ll N_r;
    ll j_sum = 0;
    FOREQ( j , 1 , M ){
      N_r = N % j;
      j_sum = ( j_sum + j - 1 ) % P;
      answer0 += ( ( N / j ) * j_sum ) % P;
      answer1 += ( N_r * ( N_r + 1 ) ) % P;
    }
    answer1 = ( answer1 % P ) * ( ( P + 1 ) / 2 );
    RETURN( ( answer0 + answer1 ) % P );
  }
  // sum( ll i = 1 ; i <= N ; i++ ) sum( ll j = 1 ; j <= M ; j++ ){ i % j }
  // = sum( ll i = 1 ; i <= N ; i++ ) sum( ll j = 1 ; j <= M ; j++ ){ i - ( i / j ) * j }
  // = M * sum( ll i = 1 ; i <= N ; i++ ){ i }
  //   - sum( ll i = 1 ; i <= N ; i++ ) sum( ll j = 1 ; j <= M ; j++ ){ ( i / j ) * j }
  // = M * ( N * ( N + 1 ) ) / 2
  //   - sum( ll j = 1 ; j <= M ; j++ ){ j * sum( ll i = 1 ; i <= N ; i++ ){ i / j } }
  // = M * ( N * ( N + 1 ) ) / 2
  //   - sum( ll j = 1 ; j <= M ; j++ ){ j * ( j * ( ( N / j - 1 ) * ( N / j ) ) / 2 + ( N - j * ( N / j ) + 1 ) * ( N / j ) ) }
  // = M * ( N * ( N + 1 ) ) / 2
  //   - sum( ll j = 1 ; j <= M ; j++ ){ j ^ 2 * ( ( ( N / j - 1 ) * ( N / j ) ) / 2 - ( N / j ) ^ 2 ) + j * ( N + 1 ) * ( N / j ) }
  // = M * ( N * ( N + 1 ) ) / 2
  //   - sum( ll j = 1 ; j <= M ; j++ ){ j * ( N + 1 ) * ( N / j ) - j ^ 2 * ( ( ( N / j ) * ( N / j + 1 ) ) / 2 ) }
  // = M * ( N * ( N + 1 ) ) / 2
  //   - sum( ll j = 1 ; j <= min( N / ( N / 31622 ) - 1 , M ) ; j++ ){ j * ( N + 1 ) * ( N / j ) - j ^ 2 * ( ( ( N / j ) * ( N / j + 1 ) ) / 2 ) }
  //   - sum( ll j = N / ( N / 31622 ) ; j <= M ; j++ ){ j * ( N + 1 ) * ( N / j ) - j ^ 2 * ( ( ( N / j ) * ( N / j + 1 ) ) / 2 ) }
  ll answer0 = ( M * ( ( ( N * ( N + 1 ) ) / 2 ) % P ) ) % P;
  ll answer1 = 0;
  ll answer2 = 0;
  ll answer3 = 0;
  constexpr const ll sqrt_bound = 31622;
  ll h = N / sqrt_bound;
  // N / j > h
  // <=> N / j >= h + 1
  // <=> N / ( 1.0 * j ) >= h + 1
  // <=> N >= ( h + 1 ) * j
  // <=> N / ( h + 1 ) >= j
  ll border = N / ( h + 1 );
  if( M > N ){
    M = N;
  }
  if( border > M ){
    border = M;
  }
  FOREQ( j , 1 , border ){
    answer1 += ( j * ( ( N + 1 ) * ( N / j ) - j * ( ( ( N / j ) * ( N / j + 1 ) ) / 2 ) ) ) % P;
  }
  answer1 %= P;
  ll j_prev = border;
  ll j_curr = ( h > 0 ? N / h : N + 1 );
  ll sum_prev = j_prev * ( j_prev + 1 );
  ll sum_curr;
  ll square_sum_prev = ( j_prev * ( j_prev + 1 ) * ( 2 * j_prev + 1 ) ) % P;
  ll square_sum_curr;
  while( j_curr <= M ){
    sum_curr = j_curr * ( j_curr + 1 );
    square_sum_curr = ( j_curr * ( ( ( j_curr + 1 ) * ( 2 * j_curr + 1 ) ) % P ) ) % P;
    answer2 += ( ( ( sum_curr - sum_prev ) % P ) * h ) % P;
    answer3 += ( ( ( ( ( square_sum_curr + P - square_sum_prev ) % P ) * h ) % P ) * ( h + 1 ) ) % P;
    j_prev = j_curr;
    sum_prev = sum_curr;
    square_sum_prev = square_sum_curr;
    // h = N / j
    // <=> N / ( i.0 * j ) - 1 < h <= N / ( 1.0 * j )
    // <=> N - j < h * j <= N
    // <=> h * j <= N < ( h + 1 ) * j
    // <=> N / ( 1.0 * ( h + 1 ) ) < j <= N / ( 1.0 * h )
    // <=> N / ( 1.0 * ( h + 1 ) ) < j <= N / h
    // exists j[ h = N / j ]
    // <=> N / ( 1.0 * ( h + 1 ) ) < N / h
    // <=> N < ( N / h ) * ( h + 1 )
    if( h > 1 && j_curr < M ){
      h--;
      j_curr = N / h ;
      while( N >= j_curr * ( h + 1 ) ){
	h--;
	j_curr = N / h ;
      }
      if( j_curr > M ){
	j_curr = M;
      }
    } else {
      break;
    }
  }
  constexpr const ll inv_2 = ( P + 1 ) / 2;
  constexpr const ll inv_3 = P - ( ( P / 3 ) * inv_2 ) % P;
  constexpr const ll inv_12 = ( ( ( inv_2 * inv_2 ) % P ) * inv_3 ) % P;
  answer2 = ( ( ( ( N + 1 ) * ( answer2 % P ) ) % P ) * inv_2 ) % P;
  answer3 = ( ( answer3 % P ) * inv_12 ) % P;
  RETURN( ( answer0 + ( P - answer1 ) + ( P - answer2 ) + answer3 ) % P );
}
0