1594B - Special Numbers - CodeForces Solution


bitmask math *1100

Please click on ads to support us..

Python Code:

class Solution:
    def specialNumber(self, n, k):
        k = bin(k)[2:]
        
        k = k[::-1]
      
        count = 0
        ans = 0
        for i in range(len(k)):
            if k[i] == "0":
                count+=1
                continue
            ans+= n**count
            count+=1
        return ans%(10**9 + 7)
    
t = int(input())
obj = Solution()
 
for _ in range(t):
    n, k = list(map(int, input().split()))
    print(obj.specialNumber(n, k))

C++ Code:

    // Coder: Luis Miguel Casañ González
    #include<bits/stdc++.h>
    
    using namespace std;
    typedef long long ll;
    ll MOD = 1e9+7;

    ll pot(int n, int p)
    {
        if(p == 0) return 1;
        
        ll ans = pot(n, p/2);
        ans = (ans*ans)%MOD;
        
        if(p&1) ans = (n*ans)%MOD;
    return ans;
    }


    ll sol()
    {
        int n, k;
        cin >> n >> k;

        ll ans = 0;
        for(int i = 0; (1<<i) <= k; i++)
        {
            if(k&(1<<i))
                ans = (ans + pot(n, i))%MOD; 
        }
    return ans;    
    }
    int main()
    {   
        #ifdef DEBUG 
            freopen("a.in", "r", stdin);
        #else
            ios_base::sync_with_stdio(0);
            cin.tie(0);
        #endif

        
        int t;
        cin >> t;

        while (t--)
        {
            cout << sol() << "\n";
        }
        
        
 
    }


Comments

Submit
0 Comments
More Questions

432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=