forked from purushottamnawale/geeksforgeeks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Modulo.cpp
More file actions
46 lines (41 loc) · 748 Bytes
/
Binary Modulo.cpp
File metadata and controls
46 lines (41 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//{ Driver Code Starts
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution
{
public:
int modulo(string s, int m)
{
int ans = 0, base = 1;
for (int i = s.length() - 1; i >= 0; i--)
{
if (s[i] == '1')
{
ans += base;
}
ans %= m;
base *= 2;
base %= m;
}
return ans;
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin >> t;
while (t--)
{
string s;
int m;
cin >> s >> m;
Solution a;
cout << a.modulo(s, m) << endl;
}
return 0;
}
// } Driver Code Ends