CoderSadanand's blog

https://www.spoj.com/problems/LOCKER/ Please tell me why I am getting wrong answer on my code

By CoderSadanand, history, 111 minutes ago, In English

include <bits/stdc++.h>

using namespace std;

long long MOD (1e9+7); long long binary_expo(long long a, long long b){ if(b < 0) return 1; long long product = 1; while(b){ if(b & 1) product = (product * a) % MOD; a = (a * a) % MOD; b >>= 1; } return product; }

int locker(int n){ long long answer = 1; if(n == 1) return answer; if(n < 3) return n; if(n % 3 == 0) { return (binary_expo(3, n / 3) % MOD); } if(n % 3 == 1){ n -= 4; return (4 * binary_expo(3,n / 3) % MOD); } else { n -= 2; return (2 * binary_expo(3, n / 3) % MOD); } }

int main() { int t; cin>>t; while(t--){ int n; cin>>n; cout<<locker(n)<<endl; } return 0; }

  • Vote: I like it
  • -1
  • Vote: I do not like it