O(1337) cpp solution beating 98%


  • 0
    T
     int modB(const vector<int> &b, int factor){
            const int N = b.size();
            int tres = 0;
            for(int bi=0; bi < N; bi++){
                tres = tres * 10 + b[bi];
                tres = tres % factor;
            }
            return tres;
        }
        
        int superPow(int a, vector<int>& b) {
            const int BASEN = 1337;
            a = a % BASEN;
            int ra(a), i(0);
            vector<int> nums(BASEN);
            for(i = 0; i < BASEN; ++i){
                nums[i] = ra;
                ra = (a * ra) % BASEN;
                if(ra == a) break;
            }
            int remaind = modB(b, i+1);
            return remaind == 0 ? nums[i] : nums[remaind-1];
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.