-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary modulo
43 lines (35 loc) · 839 Bytes
/
Binary modulo
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
//{ Driver Code Starts
//Initial Template for Java
import java.util.*;
import java.io.*;
import java.lang.*;
class GFG{
public static void main(String [] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test = Integer.parseInt(br.readLine());
while(test-- > 0) {
String [] str = br.readLine().trim().split(" ");
String s = str[0];
int m = Integer.parseInt(str[1]);
Solution obj = new Solution();
System.out.println(obj.modulo(s,m));
}
}
}
// } Driver Code Ends
//User function Template for Java
class Solution{
int modulo(String s, int m) {
//Write your code here
int ans=0,temp=1;
for(int i=s.length()-1;i>=0;i--){
if(s.charAt(i)=='1'){
ans+=temp;
ans%=m;
}
temp*=2;
temp%=m;
}
return ans;
}
}