# Binary mod and divide

2018-01-27 19:12:27来源:cnblogs.com作者:普通人i人点击

## 4030: Binary mod and divide

Most people know that the binary operations. Do you know the binary mod and divide?

Now give the Binary number N and a integer number M ,Can you tell me the answer of N%(2^M) and N/(2^M)?

Input contains multiple test cases.

The first line of each test case contains an binary number N no more than 128 bits and an integer M (1 <= M <= 64).

when N=0&&M=0 ,test is over.

output the answer the N%(2^M) and N/(2^M).

111 2

1111 2

0 0

3

3 2
4 1

` 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 using namespace std; 5 char a[150]; 6 void add(int l,int r,int b[]) 7 { 8     for(int i=l;i<r;i++) 9     {10         for(int j=0;j<40;j++)11             b[j]*=2;12         b[0]+=a[i]-'0';13         for(int j=0;j<40;j++)14         {15             if(b[j]>=10)16             {17                 b[j+1]+=b[j]/10;18                 b[j]%=10;19             }20         }21     }22 }23 int main()24 {25     int m,i,l;26     int b[45],c[45];27     while(scanf("%s%d",a,&m))28     {29         if(!m&&a[0]=='0') break;30         memset(b,0,sizeof(b));31         memset(c,0,sizeof(c));32         l=strlen(a);33         add(0,l-m,b);34         add(max(0,l-m),l,c);35         printf("mod=");36         for(i=39;i>=0;i--)37             if(c[i]) break;38         if(i==-1) printf("0");39         for(;i>=0;i--)40             printf("%d",c[i]);41         printf(", divide=");42         for(i=39;i>=0;i--)43             if(b[i]) break;44         if(i==-1) printf("0");45         for(;i>=0;i--)46             printf("%d",b[i]);47         putchar(10);48     }49 }`