数字游戏续

数字游戏续

Time Limit : 12000/4000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 152   Accepted Submission(s) : 50

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

自从小艾上次在和小牛的游戏中取得了信心之后,这次他又想到了一个问题,他要去计算两个数A和B的最大公约数。
由于这两个数非常的大,于是小牛把数字A拆成了N个数,这N个数相乘的结果是A,把B拆成了M个数,同样这M个数的乘积等于B,小艾迫不及待地想去计算出这两个数的最大公约数,这次你能帮帮他吗?如果结果超过了9位数,输出最后的9位数。

Input

输入有多组数据。
第一行包含一个正整数N ( 1 <= N <= 1000 )。
第二行包含N个以空格隔开的小于 1 000 000 000的正整数,它们的乘积是A。
第三行包含一个正整数M ( 1 <= M <= 1000 )。
第四行包含M个以空格隔开的小于 1 000 000 000的正整数,它们的乘积是B。

Output

输出A和B的最大公约数,如果结果超过了9位数,输出后9位。

Sample Input

3
2 3 5
2
4 5
4
6 2 3 4
1
1
3
358572 83391967 82
3
50229961 1091444 8863

Sample Output

10
1
000012028
View Code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

unsigned long long gcd( long long n, long long m )
{
  return m ? gcd(m,n%m): n;     
     
}

unsigned long long a[1010],s1;
unsigned long long d[1010],s2;

int main( )
{
  int N, M;
  
  unsigned long long dd[1010];
  while( scanf("%d",&N) != EOF)
  {
     s1 = 1;
     for( int i = 1; i <= N; i++)
     {
          scanf("%lld",&a[i]);
     }
     s2 = 1;
     scanf("%d",&M);
     for( int i = 1; i <= M; i++)
     {
          scanf("%lld",&d[i]);
     }
     int flag = 0;
     for( int i = 1; i <= N; i++)
     {
        for( int j = 1; j <= M; j++)
        {
           s2  = gcd(a[i],d[j]);
           s1 *= s2;      
           if( s1 >= 1000000000 )
             s1 %= 1000000000, flag = 1;
           a[i] /= s2;
           d[j] /= s2;
        }
             
     }
     if( flag )
     {
        printf("%09I64u\n",s1%1000000000); 
         
     }
     else
       printf("%I64u\n",s1);            
         
  }    
    
}

 

posted on 2012-07-24 16:21  more think, more gains  阅读(221)  评论(0编辑  收藏  举报

导航