QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#135521 | #5744. Palindromic Differences | tselmegkh | WA | 3ms | 14292kb | C++20 | 3.1kb | 2023-08-05 17:00:20 | 2023-08-05 17:00:24 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iomanip>
using namespace std;
const int N = 2e5 + 5, inf = 1e9, mod = 1e9 + 9;
#define pb push_back
#define mp make_pair
#define ll long long
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define sz(x) (int)x.size()
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
ll ba[1000006];
ll hort[1000006];
ll binpow(ll a, ll b, ll m){
a %= m;
ll res = 1;
while(b > 0){
if(b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
ll inverse(ll x){
return binpow(x, mod - 2, mod);
}
void solve(){
ll n;
cin >> n;
vi a(n);
ll sum = 0;
ll ans = 1;
map<int, int> mp;
for(ll i = 0; i < n; i++){
cin >> a[i];
mp[a[i]]++;
sum += a[i];
}
if(n % 2 == 1){
if(sum % n != 0){
cout << 0 << '\n';
return;
}
else sum = sum / n * 2;
if(mp[sum / 2] == 0){
cout << 0 << '\n';
return;
}
mp[sum / 2]--;
map<int, int> boo;
for(int i = 0; i < n; i++){
ll cnt = 0;
cnt = mp[a[i]];
if(boo[a[i]] == 1) continue;
if(a[i] * 2 == sum){
if(a[i] % 2 == 1){
cout << 0 << '\n';
return;
}
cnt = 1;
boo[a[i]] = 1;
}
if(mp[sum - a[i]] != mp[a[i]]){
cout << -1 << '\n';
return;
}
ans *=(1LL * hort[mp[a[i]]]);
ans %= mod;
ans *= inverse(ba[cnt]);
boo[sum - a[i]] = 1;
boo[a[i]] = 1;
}
}
else{
ll tmp = n / 2;
if(sum % tmp != 0){
cout << 0 << '\n';
return;
}
else sum = sum / tmp;
map<int, int> boo;
for(int i = 0; i < n; i++){
ll cnt = 0;
if(boo[a[i]] == 1) continue;
if(mp[sum - a[i]] != mp[a[i]]){
cout << -1 << '\n';
return;
}
cnt = mp[a[i]];
ans *=(1LL * hort[mp[a[i]]]);
ans %= mod;
ans *= inverse(ba[cnt]);
boo[sum - a[i]] = 1;
boo[a[i]] = 1;
}
}
while(ans < 0) ans += mod;
cout << ans << '\n';
}
int main(){
// ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t = 1;
ba[1] = 1;
hort[0] = 1;
for(int i = 2; i <= 5 * 1e5 + 5; i++){
ba[i] = ba[i - 1] * i;
ba[i] %= mod;
}
for(int i = 1; i <= 5 * 1e5 + 5; i++){
hort[i] = hort[i - 1] * 2;
hort[i] %= mod;
}
cin >> t;
while(t--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 14292kb
input:
5 3 2 3 1 4 1 1 1 1 3 1 2 4 7 0 200 0 200 50 100 150 14 -1 0 1 2 3 4 5 6 7 8 9 10 11 12
output:
2 15333333472 0 4 128
result:
wrong answer 2nd numbers differ - expected: '1', found: '15333333472'