結果

問題 No.2127 Mod, Sum, Sum, Mod
ユーザー 👑 p-adicp-adic
提出日時 2022-10-28 15:10:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 4,504 bytes
コンパイル時間 1,850 ms
コンパイル使用メモリ 201,772 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-05 19:38:52
合計ジャッジ時間 2,666 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
5,376 KB
testcase_04 AC 1 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 1 ms
5,376 KB
testcase_10 AC 1 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 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 

#define CHECK_REDUNDANT_INPUT string VARIABLE_FOR_CHECK_REDUNDANT_INPUT = ""; cin >> VARIABLE_FOR_CHECK_REDUNDANT_INPUT; assert( VARIABLE_FOR_CHECK_REDUNDANT_INPUT == "" ); assert( ! cin );
// #define CHECK_REDUNDANT_INPUT string VARIABLE_FOR_CHECK_REDUNDANT_INPUT = ""; getline( cin , VARIABLE_FOR_CHECK_REDUNDANT_INPUT ); assert( VARIABLE_FOR_CHECK_REDUNDANT_INPUT == "" ); assert( ! cin );

int main()
{
  UNTIE;
  constexpr const ll bound = 1000000000;
  CIN_ASSERT( N , 1 , bound );
  CIN_ASSERT( M , 1 , bound );
  CHECK_REDUNDANT_INPUT;
  // 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 ) }
  constexpr const ll P = 998244353;
  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