結果
問題 | No.2127 Mod, Sum, Sum, Mod |
ユーザー | 👑 p-adic |
提出日時 | 2022-10-28 15:05:30 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 4,484 bytes |
コンパイル時間 | 2,123 ms |
コンパイル使用メモリ | 201,612 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-05 19:35:11 |
合計ジャッジ時間 | 6,157 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | RE | - |
testcase_01 | RE | - |
testcase_02 | AC | 3 ms
5,376 KB |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | RE | - |
testcase_18 | RE | - |
testcase_19 | RE | - |
testcase_20 | RE | - |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 4 ms
5,376 KB |
testcase_23 | AC | 3 ms
5,376 KB |
testcase_24 | RE | - |
testcase_25 | AC | 3 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 | RE | - |
ソースコード
#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 = N / h; 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 ); }