QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#177715#6417. Classical Summation Problempikachu_coderAC ✓397ms11060kbC++142.5kb2023-09-13 11:03:112023-09-13 11:03:13

Judging History

你现在查看的是最新测评结果

  • [2023-09-13 11:03:13]
  • 评测
  • 测评结果:AC
  • 用时:397ms
  • 内存:11060kb
  • [2023-09-13 11:03:11]
  • 提交

answer

/*
3 2

Ev = sum(value of outcome i/possibility of outcome i) for all i
Ev of city number i = sum((value of city i*p(i))/2^k) where p(i) is the probability of choosing city i as the median, all p(i) sum to one! 

Because the probability distributions summing to 1 are symmetrical, the result is the center. 
*/
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define pii pair<int, int>
#define pil pair<int, ll>

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>,
rb_tree_tag, tree_order_statistics_node_update>;
//ordered_set<int> st; ordered set
//gp_hash_table<int, int> table; faster hash table
const ll mod = 998244353LL;
const int maxn = 1000001; 
vl facts(maxn, 1LL);

ll pw(ll a, ll b){ //a^b
    if(b == 0) return 1LL; 
    ll half = pw(a, b/2);
    if(b%2 == 0) return (half*half)%mod; 
    return (((half*half)%mod)*a)%mod; 
}
ll dvd(ll a, ll b){
    return (a*pw(b, mod-2LL))%mod; 
}
ll choose(int a, int b){ //a choose b, a!/(b!*(a-b)!), const factor of ~30 so use wisely
    if(b > a) return 0LL; 
    ll num = facts[a]; 
    ll den = (facts[b] * facts[a-b])%mod; 
    return dvd(num, den);
}

ll n; ll k; //n cities, k friends
//all friends meet at the median 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    for(int a = 1; a < maxn; a++) facts[a] = (facts[a-1]*(ll)a)%mod; 
	cin >> n >> k; 
    if(k%2 == 0){ //given that there is an even number of friends, the floor between the two edge friends becomes the median --> 
        //subtract expected shift left distance - ((n+1)*n^k - e(d))/2
        //how much does road i...i+1 contribute to the answer? 
        ll sm = 0LL; ll k2 = k/2LL; 
        for(int i = 1; i < n; i++){
            //k/2 to the left, k/2 to the right, 3rd city left 4th city right
            ll e = (choose(k, k2) * pw(i, k2))%mod; 
            e = (e * pw(n-i, k2))%mod; 
            sm = (sm + e)%mod; 
        }
        ll rt = ((n+1LL)*pw(n, k))%mod; 
        cout << dvd((rt-sm+mod)%mod, 2LL);
    }
    else{ //given median at whole number position i, floor(k/2) friends meet at either end OR live at position i along with the middle friend --> all arrangements are unique to the i (a single arrangement cannot represent two different medians)
        cout << (dvd(n+1LL, 2LL) * pw(n, k))%mod << "\n";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 6ms
memory: 10872kb

input:

3 2

output:

14

result:

ok 1 number(s): "14"

Test #2:

score: 0
Accepted
time: 6ms
memory: 10924kb

input:

5 3

output:

375

result:

ok 1 number(s): "375"

Test #3:

score: 0
Accepted
time: 6ms
memory: 10988kb

input:

2 2

output:

5

result:

ok 1 number(s): "5"

Test #4:

score: 0
Accepted
time: 6ms
memory: 10592kb

input:

10 9

output:

508778235

result:

ok 1 number(s): "508778235"

Test #5:

score: 0
Accepted
time: 6ms
memory: 10964kb

input:

69 3

output:

11497815

result:

ok 1 number(s): "11497815"

Test #6:

score: 0
Accepted
time: 3ms
memory: 10876kb

input:

994 515

output:

33689623

result:

ok 1 number(s): "33689623"

Test #7:

score: 0
Accepted
time: 8ms
memory: 10992kb

input:

4476 6182

output:

114894183

result:

ok 1 number(s): "114894183"

Test #8:

score: 0
Accepted
time: 6ms
memory: 10976kb

input:

58957 12755

output:

932388891

result:

ok 1 number(s): "932388891"

Test #9:

score: 0
Accepted
time: 77ms
memory: 10896kb

input:

218138 28238

output:

392861201

result:

ok 1 number(s): "392861201"

Test #10:

score: 0
Accepted
time: 236ms
memory: 10992kb

input:

644125 316810

output:

420621854

result:

ok 1 number(s): "420621854"

Test #11:

score: 0
Accepted
time: 251ms
memory: 10872kb

input:

612914 505428

output:

973686286

result:

ok 1 number(s): "973686286"

Test #12:

score: 0
Accepted
time: 6ms
memory: 10956kb

input:

998216 938963

output:

251335926

result:

ok 1 number(s): "251335926"

Test #13:

score: 0
Accepted
time: 7ms
memory: 10648kb

input:

990516 996933

output:

549551960

result:

ok 1 number(s): "549551960"

Test #14:

score: 0
Accepted
time: 397ms
memory: 10976kb

input:

999019 999012

output:

637189128

result:

ok 1 number(s): "637189128"

Test #15:

score: 0
Accepted
time: 370ms
memory: 11060kb

input:

999928 999950

output:

185229465

result:

ok 1 number(s): "185229465"

Test #16:

score: 0
Accepted
time: 3ms
memory: 10924kb

input:

999999 999999

output:

384164916

result:

ok 1 number(s): "384164916"

Test #17:

score: 0
Accepted
time: 369ms
memory: 10604kb

input:

999999 1000000

output:

696165930

result:

ok 1 number(s): "696165930"

Test #18:

score: 0
Accepted
time: 6ms
memory: 10888kb

input:

1000000 999999

output:

219071706

result:

ok 1 number(s): "219071706"

Test #19:

score: 0
Accepted
time: 360ms
memory: 10920kb

input:

1000000 1000000

output:

128206597

result:

ok 1 number(s): "128206597"

Test #20:

score: 0
Accepted
time: 3ms
memory: 10960kb

input:

2 10

output:

1410

result:

ok 1 number(s): "1410"

Test #21:

score: 0
Accepted
time: 3ms
memory: 10900kb

input:

84 16

output:

297627153

result:

ok 1 number(s): "297627153"

Test #22:

score: 0
Accepted
time: 6ms
memory: 10892kb

input:

643 800

output:

489237163

result:

ok 1 number(s): "489237163"

Test #23:

score: 0
Accepted
time: 6ms
memory: 10804kb

input:

9903 880

output:

595167333

result:

ok 1 number(s): "595167333"

Test #24:

score: 0
Accepted
time: 36ms
memory: 10968kb

input:

97446 89750

output:

410205549

result:

ok 1 number(s): "410205549"

Test #25:

score: 0
Accepted
time: 74ms
memory: 10964kb

input:

186460 646474

output:

32638530

result:

ok 1 number(s): "32638530"

Test #26:

score: 0
Accepted
time: 198ms
memory: 10976kb

input:

508940 244684

output:

598321755

result:

ok 1 number(s): "598321755"

Test #27:

score: 0
Accepted
time: 217ms
memory: 10908kb

input:

583646 557758

output:

858695621

result:

ok 1 number(s): "858695621"

Test #28:

score: 0
Accepted
time: 367ms
memory: 10928kb

input:

969610 992608

output:

256683498

result:

ok 1 number(s): "256683498"

Test #29:

score: 0
Accepted
time: 375ms
memory: 10648kb

input:

995106 996434

output:

411791999

result:

ok 1 number(s): "411791999"

Test #30:

score: 0
Accepted
time: 370ms
memory: 10860kb

input:

999961 999872

output:

61222370

result:

ok 1 number(s): "61222370"

Test #31:

score: 0
Accepted
time: 386ms
memory: 10920kb

input:

999977 999908

output:

831096762

result:

ok 1 number(s): "831096762"

Test #32:

score: 0
Accepted
time: 388ms
memory: 11056kb

input:

999992 999998

output:

562977678

result:

ok 1 number(s): "562977678"

Test #33:

score: 0
Accepted
time: 220ms
memory: 10904kb

input:

1000000 2

output:

118436113

result:

ok 1 number(s): "118436113"

Test #34:

score: 0
Accepted
time: 7ms
memory: 10924kb

input:

2 1000000

output:

298823641

result:

ok 1 number(s): "298823641"