Submission #391035

#TimeUsernameProblemLanguageResultExecution timeMemory
391035shahriarkhanThree Friends (BOI14_friends)C++14
0 / 100
382 ms11316 KiB
#include<bits/stdc++.h> using namespace std ; const int mx = 3e6 ; int a[mx] , n ; string s ; long long gcd(long long a , long long b , long long &x , long long &y) { if(!a) { x = 0 , y = 1 ; return b ; } long long x1 , y1 ; long long d = gcd(b%a,a,x1,y1) ; x = y1 - (b/a)*x1 ; y = x1 ; return d ; } long long modinverse(long long a , long long m) { long long x , y ; long long g = gcd(a,m,x,y) ; return (x%m + m)%m ; } void f(long long mod) { long long P = 1 , first = 0 , second = 0 , c_first = 0 , c_second = 0 , tot = 0 , div = modinverse(31,mod) ; //precompute 0..(n/2) for(int i = 0 ; i <= (n/2) ; ++i) { long long c_cnt = (s[i] - 'A' + 1) ; first = (first + (c_cnt*P)%mod)%mod ; P = (P*31LL)%mod ; } //precompute (n/2)..(n-1) P = 1 ; for(int i = (n/2) ; i < n ; ++i) { long long c_cnt = (s[i] - 'A' + 1) ; second = (second + (c_cnt*P)%mod)%mod ; P = (P*31LL)%mod ; } //precompute 0..((n/2) - 1) P = 1 ; for(int i = 0 ; i < (n/2) ; ++i) { long long c_cnt = (s[i] - 'A' + 1) ; c_first = (c_first + (c_cnt*P)%mod)%mod ; P = (P*31LL)%mod ; } //precompute (n/2 + 1)..(n-1) P = 1 ; for(int i = (n/2) + 1 ; i < n ; ++i) { long long c_cnt = (s[i] - 'A' + 1) ; c_second = (c_second + (c_cnt*P)%mod)%mod ; P = (P*31LL)%mod ; } //check if it is in the range (0...((n/2)-1)) P = 1 ; for(int i = 0 ; i < (n/2) ; ++i) { long long c_cnt = (s[i] - 'A' + 1) ; tot = (tot + (c_cnt*P)%mod)%mod ; long long cur = (((first-tot+mod)%mod)*div)%mod ; cur = (cur + tot - (c_cnt*P)%mod + mod)%mod ; if(cur==c_second) a[i] &= 1 ; else a[i] = 0 ; P = (P*31LL)%mod ; } //check if it is in the range ((n/2)...(n-1)) tot = 0 , P = 1 ; for(int i = n/2 ; i < n ; ++i) { long long c_cnt = (s[i] - 'A' + 1) ; tot = (tot + (c_cnt*P)%mod)%mod ; long long cur = (((second-tot+mod)%mod)*div)%mod ; cur = (cur + tot - (c_cnt*P)%mod + mod)%mod ; if(cur==c_first) a[i] &= 1 ; else a[i] = 0 ; P = (P*31LL)%mod ; } } int main() { int ans = -1 , cnt = 0 ; scanf("%d",&n) ; cin>>s ; if(!(n&1)) { puts("NOT POSSIBLE") ; return 0 ; } for(int i = 0 ; i < n ; ++i) a[i] = 1 ; long long cur = 1e9 + 7 ; f(cur) ; cur = 1e9 + 9 ; f(cur) ; for(int i = 0 ; i < n ; ++i) { if(a[i]) ans = i ; cnt += a[i] ; } if(!cnt) { puts("NOT POSSIBLE") ; } else if(cnt==1) { for(int i = 0 ; i < (n/2) + (ans<(n/2)) ; ++i) { if(i==ans) continue ; cout<<s[i] ; } cout<<endl ; } else { puts("NOT UNIQUE") ; } return 0 ; }

Compilation message (stderr)

friends.cpp: In function 'long long int modinverse(long long int, long long int)':
friends.cpp:27:15: warning: unused variable 'g' [-Wunused-variable]
   27 |     long long g = gcd(a,m,x,y) ;
      |               ^
friends.cpp: In function 'int main()':
friends.cpp:94:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   94 |     scanf("%d",&n) ;
      |     ~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...