Submission #391018

#TimeUsernameProblemLanguageResultExecution timeMemory
391018shahriarkhanThree Friends (BOI14_friends)C++14
0 / 100
132 ms6216 KiB
#include<bits/stdc++.h>
using namespace std ;

const long long mod = 1e9 + 7 ;

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 ;
}

int main()
{
    int n , ans = -1 , cnt = 0 ;
    scanf("%d",&n) ;
    string s ;
    cin>>s ;
    if(!(n&1))
    {
        puts("NOT POSSIBLE") ;
        return 0 ;
    }
    long long P = 1 , first = 0 , second = 0 , c_first = 0 , c_second = 0 , tot = 0 , div = modinverse(31,mod) ;
    for(int i = 0 ; i <= (n/2) ; ++i)
    {
        if(i==(n/2)) c_first = first ;
        first = (first + ((s[i]-'A'+1)*P)%mod)%mod ;
        P = (P*31LL)%mod ;
    }
    P = 1 ;
    for(int i = (n/2) ; i < n ; ++i)
    {
        second = (second + ((s[i]-'A'+1)*P)%mod)%mod ;
        P = (P*31LL)%mod ;
    }
    P = 1 ;
    for(int i = (n/2) + 1 ; i < n ; ++i)
    {
        c_second = (c_second + ((s[i]-'A'+1)*P)%mod)%mod ;
        P = (P*31LL)%mod ;
    }
    P = 1 ;
    for(int i = 0 ; i < (n/2) ; ++i)
    {
        tot = (tot + ((s[i]-'A'+1)*P)%mod)%mod ;
        long long cur = (tot - ((s[i]-'A'+1)*P)%mod + (((first-tot+mod)%mod)*div)%mod + mod)%mod ;
        if(cur==c_second)
        {
            ans = i ;
            ++cnt ;
        }
        P = (P*31LL)%mod ;
    }
    tot = 0 , P = 1 ;
    for(int i = n/2 ; i < n ; ++i)
    {
        tot = (tot + ((s[i]-'A'+1)*P)%mod)%mod ;
        long long cur = (tot - ((s[i]-'A'+1)*P)%mod + (((first - tot+mod)*div)%mod + mod))%mod ;
        if(cur==c_first)
        {
            ans = i ;
            ++cnt ;
        }
        P = (P*31LL)%mod ;
    }
    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:23:15: warning: unused variable 'g' [-Wunused-variable]
   23 |     long long g = gcd(a,m,x,y) ;
      |               ^
friends.cpp: In function 'int main()':
friends.cpp:30:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   30 |     scanf("%d",&n) ;
      |     ~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...