• + 0 comments

    I don't know where this code is wrong. I modularized it, but the answer is different.

    static long mod(long x){
        while(x<0)
            x+=1000000007;
        return (x%1000000007);    
    }
    public static long countArray(int n, int k, int x)
    {
    // Return the number of ways to fill in the array.
        if(k<=1 || n<2)return 0;
        if(k==2 && n<=3) return 0;
        if(k==2)
        {
            if(x==1 ) 
            {
                if( n%2==0)  return 0;
                return 1;
            }
            if(x==2)
            {
                if( n%2==1)  return 0;
                return 1;
            }
        }
    
        long result= 1;
        int N=n-2;
    
        for(int i=0;i<N;i++)
        {
            result = mod(result*(k-1));
        }
        //result=mod(result*k);
        Console.WriteLine("result : "+result);
        long kn=1;
        for(int i=0;i<N-1;i++)
        {
            kn = mod(kn*(k-1));
        }
        Console.WriteLine("kn : "+kn);
        //result=mod(result-2*kn);
        Console.WriteLine("result : "+result);
        long ss1=0;
        long ss2=0;
    
            ss1 =mod(((kn+(k-1)*(((N-1)%2==0)?1:-1))/k));
            ss2 =mod(((kn+(((N)%2==0)?1:-1))/k));
        if(x==1)
        {
            kn=mod(ss2*(k-1));
        Console.WriteLine("ss1 : "+kn);
        }
        else 
        {
            kn=mod(ss2*(k-2)+ss1);
        Console.WriteLine("ss2 : "+kn);
        }
        return mod(result -kn);
    }