二进制求最大公约数&&输出二进制

Divided Land

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 56    Accepted Submission(s): 27 

Problem Description

It’s time to fight the local despots and redistribute the land. There is a rectangular piece of land granted from the government, whose length and width are both in binary form. As the mayor, you must segment the land into multiple squares of equal size for the villagers. What are required is there must be no any waste and each single segmented square land has as large area as possible. The width of the segmented square land is also binary.

Input

The first line of the input is T (1 ≤ T ≤ 100), which stands for the number of test cases you need to solve.
   Each case contains two binary number represents the length L and the width W of given land. (0 < L, W ≤ 21000)

OutputFor each test case, print a line “Case #t: ”(without quotes, t means the index of the test case) at the beginning. Then one number means the largest width of land that can be divided from input data. And it will be show in binary. Do not have any useless number or space.

Sample Input

3 10 100 100 110 10010 1100

Sample Output

Case #1: 10 Case #2: 10 Case #3: 110

Source

2014 ACM/ICPC Asia Regional Shanghai Online


#include <stdio.h>
#include <string.h>
#define MAXN 1000
struct BigNumber
{
    int len;
    int v[MAXN];
};
bool isSmaller(BigNumber n1,BigNumber n2)
{
    if(n1.len<n2.len)
        return 1;
    if(n1.len>n2.len)
        return 0;
    for(int i=n1.len-1; i>=0; i--)
    {
        if(n1.v[i]<n2.v[i])
            return 1;
        if(n1.v[i]>n2.v[i])
            return 0;
    }
    return 0;
}
BigNumber minus(BigNumber n1,BigNumber n2)
{
    BigNumber ret;
    int borrow,i,temp;
    ret=n1;
    for(borrow=0,i=0; i<n2.len; i++)
    {
        temp=ret.v[i]-borrow-n2.v[i];
        if(temp>=0)
        {
            borrow=0;
            ret.v[i]=temp;
        }
        else
        {
            borrow=1;
            ret.v[i]=temp+2;
        }
    }
    for(; i<n1.len; i++)
    {
        temp=ret.v[i]-borrow;
        if(temp>=0)
        {
            borrow=0;
            ret.v[i]=temp;
        }
        else
        {
            borrow=1;
            ret.v[i]=temp+2;
        }
    }
    while(ret.len>=1 && !ret.v[ret.len-1])
        ret.len--;
    return ret;
}
BigNumber div2(BigNumber n)
{
    BigNumber ret;
    ret.len=n.len-1;
    for(int i=0; i<ret.len; i++)
        ret.v[i]=n.v[i+1];
    return ret;
}
void gcd(BigNumber n1,BigNumber n2)
{
    long b=0,i;
    while(n1.len && n2.len)
    {
        if(n1.v[0])
        {
            if(n2.v[0])
            {
                if(isSmaller(n1,n2))
                    n2=minus(n2,n1);
                else
                    n1=minus(n1,n2);
            }
            else
                n2=div2(n2);
        }
        else
        {
            if(n2.v[0])
                n1=div2(n1);
            else
            {
                n1=div2(n1);
                n2=div2(n2);
                b++;
            }
        }
    }
    if(n2.len)
        for(i=n2.len-1; i>=0; i--)
            printf("%d",n2.v[i]);
    else
        for(i=n1.len-1; i>=0; i--)
            printf("%d",n1.v[i]);
    while(b--)
        printf("0");
    printf("\n");
}
int main()
{
    int cases,le,i;
    BigNumber n1,n2;
    char str1[MAXN],str2[MAXN];
    scanf("%d",&cases);
    int num;
    for(num=1;num<=cases;num++)
    {
        scanf("%s%s",str1,str2);
        le=strlen(str1);
        n1.len=le;
        for(i=0; i<le; i++)
            n1.v[i]=str1[le-1-i]-‘0‘;
        le=strlen(str2);
        n2.len=le;
        for(i=0; i<le; i++)
            n2.v[i]=str2[le-1-i]-‘0‘;
            printf("Case #%d: ",num);
        gcd(n1,n2);
    }
    return 0;
}
时间: 2024-08-05 20:49:55

二进制求最大公约数&&输出二进制的相关文章

hdu----(5050)Divided Land(二进制求最大公约数)

Divided Land Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 123    Accepted Submission(s): 64 Problem Description It’s time to fight the local despots and redistribute the land. There is a rect

【微软100题】输入一个整数,求该整数的二进制表达中有多少个1

package test; /** 整数的二进制表示中1的个数 题目:输入一个整数,求该整数的二进制表达中有多少个1. 例如输入10,由于其二进制表示为1010,有两个1,因此输出2. 分析: 方法一:把十进制转换成二进制字符数组,遍历该数组,判断1的个数. 方法二:对于一个int n, n&1的结果就是n转化成二进制数后的最后一位的结果.考察了位运算 包括微软在内的很多公司都曾采用过这道题. * @author Zealot * */ public class MS_28 { private

java语言将任意一个十进制数数字转换为二进制形式,并输出转换后的结果

1 package com.llh.demo; 2 3 import java.util.Scanner; 4 5 /** 6 * 7 * @author llh 8 * 9 */ 10 public class Test { 11 /* 12 * 将任意一个十进制数数字转换为二进制形式,并输出转换后的结果(使用数组存储) 13 */ 14 public static void main(String[] args) { 15 Scanner sc = new Scanner(System.in

C语言整数按照二进制逆序,输出逆序后的整数值

问题来源,今天早上和一舍友吃早餐的时候谈到的一个问题,将一个整数按照二进制逆序,然后输出逆序后的数值. 我们知道数值在内存中都是以二进制的形式存放的,假如我们是32位机,每8位为一个字节,int型在32位机上是占4个字节,即32位. 如   2  = 0000 0000 0000 0000 0000 0000 00000 0010(32位) 逆 ^2  = 0100 0000 0000 0000 0000 0000 00000 0000  (这里用^表示逆转) 那么这个操作要如何执行呢?首先补充

十进制转换为二进制序列,并输出1的个数,和序列的奇偶序列

★十进制转换为二进制序列,并输出1的个数,和序列的奇偶序列 #include<stdio.h> int main() { int m,i,x,y; char a[32];//int为4个字节长,占32个bit位 int count=0; printf("请输入一个数:\n"); scanf("%d", &m); for (i = 0; i < 32; i++) { if (m%2 == 1)         //统计序列中1的个数 { co

二分求幂(快速求幂,二进制求幂)

二分求幂, 非递归求法(二进制求法): 比如 2^5就是5个2相乘,按照5的二进制求 3^10就是8个3相乘,再2个3相乘. 处理幂的二进制,具体实现代码如下: long long quickmulti(long long a,long long b) { long long res=1; while(b) { if(b&1) //如果最后一位为1,则res*=a; res*=a; a*=a; //a*=a b>>=1; //b%=2 } return res; }

35:输出二进制补码

35:输出二进制补码 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 输入一个整型(int)的整数,输出它的32位二进制补码. 输入 一个整型整数. 输出 输出一行,即该整数的补码表示. 样例输入 7 样例输出 00000000000000000000000000000111‘ 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 int a[10001]; 5 i

c语言:获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列。

获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列. 程序: #include<stdio.h> int main() { int i, j, num; char arr1[16]; char arr2[16]; printf("输入一个整数:"); scanf("%d", &num); for (i = 0, j = 0; i < 32; i += 2, j++) { arr1[15 - j] = (num >> 

求最大公约数问题

描述 给定两个正整数,求它们的最大公约数. 输入输入一行,包含两个正整数(<1,000,000,000).输出输出一个正整数,即这两个正整数的最大公约数.样例输入 6 9 样例输出 3 提示求最大公约数可以使用辗转相除法:假设a > b > 0,那么a和b的最大公约数等于b和a%b的最大公约数,然后把b和a%b作为新一轮的输入.由于这个过程会一直递减,直到a%b等于0的时候,b的值就是所要求的最大公约数.比如:9和6的最大公约数等于6和9%6=3的最大公约数.由于6%3==0,所以最大公