SPOJ HG - HUGE GCD

题目链接http://www.spoj.com/problems/HG/

题目大意:分别给N个和M个小于等于1e9的数,前N个数相乘为A,后M个数相乘为B,问你GCD(A, B)  1<= N<= 1000,1 <=M <= 1000

解题思路:枚举a[i] 和b[i],计算两者GCD,然后结果乘以GCD,两者都除以GCD。最后判断一下如何输出即可。

代码:

 1 const int maxn = 1e4 + 5;
 2 int n, m;
 3 ll a[maxn], b[maxn];
 4
 5 int gcd(int x, int y){
 6     return y == 0? x: gcd(y, x % y);
 7 }
 8 void solve(){
 9     ll ans = 1;
10     bool flag = false;
11     for(int i = 0; i < n; i++){
12         for(int j = 0; j < m; j++){
13             int tmp = gcd(a[i], b[j]);
14             a[i] /= tmp; b[j] /= tmp;
15             ans *= tmp;
16             if(ans >= mod){
17                 flag = true;
18                 ans %= mod;
19             }
20         }
21     }
22     if(flag) printf("%09lld\n", ans % mod);
23     else printf("%lld\n", ans);
24 }
25
26 int main(){
27     scanf("%d", &n);
28     for(int i = 0; i < n; i++) scanf("%lld", &a[i]);
29     scanf("%d", &m);
30     for(int i = 0; i < m; i++) scanf("%lld", &b[i]);
31     solve();
32 }

题目:

HG - HUGE GCD

#math #number-theory

RK has received a homework assignment to compute the greatest common divisor of the two positive integers Aand B. Since the numbers are quite large, the professor provided him with N smaller integers whose product is A, and M integers with product B.

RK would like to verify his result, so he has asked you to write a program to solve his problem. If the result is more than 9 digits long, output only the last 9 digits.

INPUT

The first line of input contains the positive integer N (1<= N<= 1000).
The second line of input contains N space-separated positive integers less than 10^9, whose product is the number A.
The third line of input contains the positive integer M (1 <=M <= 1000).
The fourth line of input contains M space-separated positive integers less than 10^9, whose product is the number B.

OUTPUT

The first and only line of output must contain the greatest common divisor of numbers A and B. If the result is more than 9 digits long, output only the last (least significant) 9 digits.

SAMPLE

Input
3
2 3 5
2
4 5
Output
10

Input
3
358572 83391967 82
3
50229961 1091444 8863
Output
000012028

First sample description: The greatest common divisor of numbers A = 30 and B = 20 equals 10.

时间: 2024-07-29 04:02:26

SPOJ HG - HUGE GCD的相关文章

数论十题

数论十题 Problem Zero:[neerc2011]Gcd guessing game 现在有一个数x,1 ≤ x≤ n,告诉你n,每次你可以猜一个数y,如果x==y则结束,否则返回gcd(x,y),问最少只要几次就可以保证猜出答案. 本题纯属娱乐.仅仅是一个GCD的游戏,跑题了. 因为本题要求最坏情况,我们直观地猜想就是每次返回都是1.由于答案有可能是质数,而判定一个数,必须要把含有这个质因子的数问一遍.于是,我们引出这样一个思路,将所有1-n的质数分组,每组的积<=n,答案就是组数.

[SDOI2009]SuperGCD

[题面]: [SDOI2009]SuperGCD [思路]: 毒瘤高精.. 考这种题真不知道出题人怎么想的,高精就算了还要压八位..我高精板子都挂了还是寻欢大神给我了个板子\(qwq\) 这是一道裸(du)的(liu)\(GCD\),当你把一切运算符都重载之后,你就可以愉快地\(coding\)出来\(gcd\)!then TLE 你还需要这个:更相减损术 可半者半之,不可半者,副置分母.子之数,以少减多,更相减损,求其等也.以等数约之.\(orz\) 具体来说,就是(如果需要对分数进行约分,那

SPOJ - PGCD Primes in GCD Table(莫比乌斯反演)

http://www.spoj.com/problems/PGCD/en/ 题意: 给出a,b区间,求该区间内满足gcd(x,y)=质数的个数. 思路: 设f(n)为 gcd(x,y)=p的个数,那么F(n)为 p | gcd(x,y)的个数,显然可得F(n)=(x/p)*(y/p). 这道题目因为可以是不同的质数,所以需要枚举质数, 但是这样枚举太耗时,所以在这里令t=pk, 这样一来的话,我们只需要预处理u(t/p)的前缀和,之后像之前的题一样分块处理就可以了. 1 #include<ios

spoj 3871 gcd extreme

1 题目大意给出一个n,求sum(gcd(i,j),0<i<j<=n); 2 可以明显的看出来s[n]=s[n-1]+f[n]; 3 f[n]=sum(gcd(i,n),0<i<n); 4 现在麻烦的是求f[n] 5 gcd(x,n)的值都是n的约数,则f[n]= 6 sum{i*g(n,i),i是n的约数},注意到gcd(x,n)=i的 7 充要条件是gcd(x/i,n/i)=1,因此满足条件的 8 x/i有phi(n/i)个,说明gcd(n,i)=phi(n/i). 9

SPOJ PGCD - Primes in GCD Table (好题! 莫比乌斯反演+分块求和优化)

PGCD - Primes in GCD Table Johnny has created a table which encodes the results of some operation -- a function of two arguments. But instead of a boring multiplication table of the sort you learn by heart at prep-school, he has created a GCD (greate

* SPOJ PGCD Primes in GCD Table (需要自己推线性筛函数,好题)

题目大意: 给定n,m,求有多少组(a,b) 0<a<=n , 0<b<=m , 使得gcd(a,b)= p , p是一个素数 这里本来利用枚举一个个素数,然后利用莫比乌斯反演可以很方便得到答案,但是数据量过大,完全水不过去 题目分析过程(从别人地方抄来的) ans = sigma(p, sigma(d, μ(d) * (n/pd) * (m/pd))) Let s = pd, then ans = sigma(s, sigma(p, μ(s/p) * (n/s) * (m/s))

spoj 3871. GCD Extreme 欧拉+积性函数

3871. GCD Extreme Problem code: GCDEX Given the value of N, you will have to find the value of G. The meaning of G is given in the following code G=0; for(k=i;k< N;k++) for(j=i+1;j<=N;j++) { G+=gcd(k,j); } /*Here gcd() is a function that finds the g

SPOJ:NO GCD (求集合&amp;秒啊)

You are given N(1<=N<=100000) integers. Each integer is square free(meaning it has no divisor which is a square number except 1) and all the prime factors are less than 50. You have to find out the number of pairs are there such that their gcd is 1

Visible Lattice Points(spoj7001+初探莫比乌斯)gcd(a,b,c)=1 经典

VLATTICE - Visible Lattice Points no tags Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at (N,N,N). How many lattice points are visible from corner at (0,0,0) ? A point X is visible from point Y iff no other lattice point