Untitled

Anonymous
plain_text
02/22/2026 4:13 AM
3.6 KB
33
Indexable
========== A ==========
class Solution {
public:
    int scoreDifference(vector<int>& nums) {
        int n=nums.size();
        vector<int> player(2); // player[0] = first , player[1] = second
        int active=0; // who is active
        for(int i=1;i<=n;i++){
            int score=nums[i-1];
            if(score&1) active^=1; // 0->1, 1->0
            if(i%6==0) active^=1;
            player[active]+=score;
        }
        return player[0]-player[1];
    }
};

=========== B ============
class Solution {
public:
    bool isDigitorialPermutation(int n) {
        vector<int> fac(10);
        fac[0]=1;
        for(int i=1;i<=9;i++) fac[i]=fac[i-1]*i;
        int facSum=0,z=n;
        while(z){
            int digit=z%10;
            z/=10;
            facSum+=fac[digit];
        }
        string strN = to_string(n);
        string strFacSum = to_string(facSum);
        sort(strN.begin(),strN.end());
        sort(strFacSum.begin(),strFacSum.end());
        return strN==strFacSum;
    }
};

============= C =============
class Solution {
public:
    string maximumXor(string s, string t) {
        int n=t.size(),cnt0=0,cnt1=0;
        for(auto c:t){
            if(c=='0') cnt0++;
            else cnt1++;
        }
        string ans;
        for(int i=0;i<n;i++){ // MSB -> LSB
            if(s[i]=='1'){
                if(cnt0>0) ans+="1",cnt0--;
                else ans+="0",cnt1--;
            }
            else{
                if(cnt1>0) ans+="1",cnt1--;
                else ans+="0",cnt0--;
            }
        }
        return ans;
    }
};

================= D =================
class Solution {
public:
    int countSequences(vector<int>& nums, long long k) {
        map<tuple<int,int,int>,long long>dp;
        dp[{0,0,0}]=1; // val=1 => 2^0 * 3^0 * 5*0
        auto factor=[&](int x){
            int a=0,b=0,c=0;
            while(x%2==0) a++,x/=2;
            while(x%3==0) b++,x/=3;
            while(x%5==0) c++,x/=5;
            return tuple<int,int,int>{a,b,c}; // x = 2^a * 3^b * 5^c
        };
        for(int x:nums){
            auto[fa,fb,fc]=factor(x);
            map<tuple<int,int,int>,long long> ndp;
            for(auto &it:dp){
                auto[a,b,c]=it.first;
                long long cnt=it.second;
                ndp[{a+fa,b+fb,c+fc}]+=cnt; // mul with nums[i]
                ndp[{a-fa,b-fb,c-fc}]+=cnt; // div with nums[i]
                ndp[{a,b,c}]+=cnt; // no change
            }
            dp.swap(ndp); // dp=ndp;
        }
        int ka=0,kb=0,kc=0;
        long long t=k;
        while(t%2==0) ka++,t/=2;
        while(t%3==0) kb++,t/=3;
        while(t%5==0) kc++,t/=5;
        if(t!=1) return 0; // 2^3 * 3^1 * 5^3 * 7^1
        return dp[{ka,kb,kc}]; // number of ways to convert val to K
    }
};
Editor is loading...
Leave a Comment