1144-ray Gun
|
PDF (中文版) |
Statistics |
Forum |
Time Limit:2 second (s) |
Memory limit:32 MB |
You is in an m x n grid. You is standing in position (0, 0) and in each of the other lattice points (points with integer co-ordinates) an Enemy is waiting. Now there is a ray gun, can fire up to infinity and no obstacle can stop it. Your target is to kill all the enemies. You have a to find the minimum number of times you had to fire to kill all of them. The For a 4 x 4 grid is the fire Times. See the picture below:
Input
Input starts with an integer T (≤100), denoting the number of test cases.
Each case contains-integers m, n (0≤m, n≤109) and at least one of the them would be less than or equal to 106.
Output
For each case, print the case number and the minimum number of times that you had to fire to kill all the enemies.
Sample Input |
Output for Sample Input |
2 4 4 10 10 |
Case 1:13 Case 2:65 |
Problem Setter:jane ALAM Jan thought: Originally wanted to use Euler function, and then a look at the scope is too large, then only in [1,n] violence per number in [1,m] How many of the number with it, then the cycle of violence [1,n] and then every time to find and n the number of non-reciprocal, Then you have to count the other. Special Award n=0| | M=0 of the time.
1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <string.h>5#include <stdlib.h>6#include <queue>7#include <math.h>8#include <vector>9 using namespacestd;TentypedefLong LongLL; One BOOLprime[1000005]; A intans[1000005]; - intflag[1000005];//memory, when a number of the quality factor of the same type, to avoid duplication of operations - intfen[ -]; the intd[1000005];//maximum factorization for each number - intSlove (intNintm); - intMainvoid) - { + inti,j,k; -Fill (ans,ans+1000005,1); +Fill (d,d+1000005,1); A for(i=2; i<=1000000; i++) at { - if(!Prime[i]) - { - for(j=2; (I*J) <=1000000; J + +) - { -prime[i*j]=true; inans[i*j]*=i; -d[i*j]=i; to } + } - } the for(i=2; i<=1000000; i++) * { $ Panax Notoginseng if(!Prime[i]) - { theans[i]*=i; +d[i]=i; A } the } + ints; -scanf"%d",&k); $LL sum=0; $ intn,m; - for(s=1; s<=k; s++) -{sum=0; thememset (flag,-1,sizeof(flag)); -scanf"%d%d",&n,&m);Wuyi if(n>m) the { - swap (n,m); Wu } - if(m==0) sum=0; About Else if(n==0) $ { -sum=1; - } - Else A{sum=2; + for(i=1; i<=n; i++) the { - if(flag[ans[i]]!=-1) $ { thesum+=Flag[ans[i]]; the } the Else the { -flag[ans[i]]=Slove (i,m); insum+=Flag[ans[i]]; the } the } About } theprintf"Case %d:%lld\n", s,sum); the } the return 0; + } - intSlove (intNintm) the {Bayi inti,j,k; the intnn=N; the intCnt=0; - while(n>1) -{fen[cnt++]=D[n]; theN/=D[n]; the } the intCc=1<<CNT; theLL sum=0; - intsum1=0; the for(i=1; i<cc; i++) the { the intck=0;94 intak=1; the for(j=0; j<cnt; J + +) the { the if(i& (1<<j))98 { Aboutak*=Fen[j]; -ck++;101 }102 }103 if(ck%2)104 { the 106sum+=m/AK;107 }108 Elsesum-=m/AK;109 } the returnM-sum;111}
1144-ray Gun