1144 - Ray Gun

   PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

You are in an m x n grid. You are standing in position (0, 0) and in each of the other lattice points (points with integer co-ordinates) an enemy is waiting. Now you have a ray gun that can fire up to infinity and no obstacle can stop it. Your target is to kill all the enemies. You have to find the minimum number of times you have to fire to kill all of them. For a 4 x 4 grid you have to fire 13 times. See the picture below:

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains two integers m, n (0 ≤ m, n ≤ 109) and at least one of them will be less than or equal to 106.

Output

For each case, print the case number and the minimum number of times you have 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

思路:本来想用欧拉函数的,然后一看范围太大;那么只能在[1,n]暴力每个数在[1,m]中有多少个与它互素的数,那么暴力循环[1,n]然后每次容斥找与n不互素的数,然后得互素的数。特判n=0||m=0的时候。

  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 namespace std;
 10 typedef long long LL;
 11 bool prime[1000005];
 12 int ans[1000005];
 13 int flag[1000005];//记忆化,当一些数的质因子种类相同,避免重复运算
 14 int fen[100];
 15 int d[1000005];//每个数的最大质因数
 16 int slove(int n,int m);
 17 int main(void)
 18 {
 19     int i,j,k;
 20     fill(ans,ans+1000005,1);
 21     fill(d,d+1000005,1);
 22     for(i=2; i<=1000000; i++)
 23     {
 24         if(!prime[i])
 25         {
 26             for(j=2; (i*j)<=1000000; j++)
 27             {
 28                 prime[i*j]=true;
 29                 ans[i*j]*=i;
 30                 d[i*j]=i;
 31             }
 32         }
 33     }
 34     for(i=2; i<=1000000; i++)
 35     {
 36
 37         if(!prime[i])
 38         {
 39             ans[i]*=i;
 40             d[i]=i;
 41         }
 42     }
 43     int s;
 44     scanf("%d",&k);
 45     LL sum=0;
 46     int n,m;
 47     for(s=1; s<=k; s++)
 48     {   sum=0;
 49         memset(flag,-1,sizeof(flag));
 50         scanf("%d %d",&n,&m);
 51         if(n>m)
 52         {
 53             swap(n,m);
 54         }
 55         if(m==0)sum=0;
 56         else if(n==0)
 57         {
 58             sum=1;
 59         }
 60         else
 61         {   sum=2;
 62             for(i=1; i<=n; i++)
 63             {
 64                 if(flag[ans[i]]!=-1)
 65                 {
 66                     sum+=flag[ans[i]];
 67                 }
 68                 else
 69                 {
 70                     flag[ans[i]]=slove(i,m);
 71                     sum+=flag[ans[i]];
 72                 }
 73             }
 74         }
 75         printf("Case %d: %lld\n",s,sum);
 76     }
 77     return 0;
 78 }
 79 int slove(int n,int m)
 80 {
 81     int i,j,k;
 82     int nn=n;
 83     int cnt=0;
 84     while(n>1)
 85     {fen[cnt++]=d[n];
 86         n/=d[n];
 87     }
 88     int cc=1<<cnt;
 89     LL sum=0;
 90     int sum1=0;
 91     for(i=1; i<cc; i++)
 92     {
 93         int ck=0;
 94         int ak=1;
 95         for(j=0; j<cnt; j++)
 96         {
 97             if(i&(1<<j))
 98             {
 99                 ak*=fen[j];
100                 ck++;
101             }
102         }
103         if(ck%2)
104         {
105
106             sum+=m/ak;
107         }
108         else sum-=m/ak;
109     }
110     return m-sum;
111 }
时间: 2024-08-10 02:10:44

1144 - Ray Gun的相关文章

Adobe Photoshop Lightroom v5.4 MacOSX 专业摄影师图像处理软件

AirRadar 2.3.3 MacOSX Viscosity 1.4.8 MacOSX Iridient Developer 2.3.4 Mac OS X SideFX Houdini FX 13.0.376 Win/Mac/Linux Vinotekasoft Vinoteka 3.3.5 Multilingual MacOSX Firetask 3.6.1 Multilingual MacOSX iBackup Viewer Pro 2.70 MacOSX DivX Plus Pro 10

IT英语4-计算机英语缩写术语

1.CPU 3DNow!(3D no waiting,无须等待的3D处理) AAM(AMD Analyst Meeting,AMD分析家会议) ABP(Advanced Branch Prediction,高级分支预测) ACG(Aggressive Clock Gating,主动时钟选择) AIS(Alternate Instruction Set,交替指令集) ALAT(advanced load table,高级载入表) ALU(Arithmetic Logic Unit,算术逻辑单元) 

【转】Unity3D 射线Ray实现点击拾取

游戏中经常会有鼠标移动到某个对象上来拾取它的功能,我们可以用Unity3D中的射线Ray实现这一效果.原理是在我们鼠标的位置,从屏幕射出一条射向世界空间的射线,当这条射线碰撞到我们需要拾取的对象时,我们就销毁对象,把它添加到我们的背包中. 我们来做一个简单的Demo,我们在场景中添加一个方块Cube,一个小球Sphere,当我们鼠标放在方块上时没有任何反应,而当我们把鼠标放在小球上时,小球会消失. 新建一个项目,我们命名为"RayTest",然后在场景中新建一个小球,方块,和添加灯光,

Ray射线

射线的机制相当于碰撞; 创建射线: 1 Ray ray=new Ray(); origin : 射线发射的原点; direction: 射线发射的方向; distance: 射线的长度; hitInfo: 如果返回true,hitInfo将包含碰撞器碰撞的更多信息; Layer: gameObject的层; LayerMask: 只选定LayerMask层内的碰撞器,其他层内碰撞器忽略;返回bool类型,当射线与任何碰撞器碰撞时为真,反之为假; 射线碰撞的应用: 使用Physics类方法Rayc

题目1144:Freckles(最小生成树进阶)

题目链接:http://ac.jobdu.com/problem.php?pid=1144 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: // // 1144 Freckles.cpp // Jobdu // // Created by PengFei_Zheng on 18/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #include <s

Mental Ray渲染--在摄像机动画中如何避免finalgather闪烁?

Final gathering 依赖于在finalgather点周围进行差值,以度量入射间接光照的光强.如果没有足够多的临近点,mental ray将自动增加一些.如果摄像机发生移动,这些临近点将加载不同的位置.如果部分场景很暗,或者照明的对比度较大,那么finalgather点的光照变化率就很高,这就可能导致画面闪烁. 加大accuracy的光线数量或者半径通常可以解决这个问题,这是由于光线的数量的增加而牺牲一些性能为代价的.还可以在finalgather only 模式下先运行动画,记录一个

Gun N&#39; Rose Team Review

一看到这个项目就被他的功能给吸引了.回忆起以前看到一个东西很新奇想去网上查询它是什么,但是又不知道应该怎样去描述它,于是在搜索引擎的输入框中键入.删除.键入.删除的可笑经历的时候,我就越发感觉到这个app的必要性了.画出查询对象的大致轮廓,系统自动搜筛选出相似的图片,这个app实现了“所见即所得”,令人称赞. 当然,这个app也存在很多局限性,比如说它返回的图片数量很大,没能实现精确定位,如果能让用户在输入时添加对对象的模糊描述,比如说:不是房子,像苹果等无疑将提高搜索的准确度.另外,这个app

hdu 5281 Senior&#39;s Gun

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5281 Senior's Gun Description Xuejiejie is a beautiful and charming sharpshooter. She often carries $n$ guns, and every gun has an attack power $a[i]$. One day, Xuejiejie goes outside and comes across $m

UVA 11008 Antimatter Ray Clearcutting(DP)

It's year 2465, and you are the Chief Engineer for Glorified Lumberjacks Inc. on planet Trie. There is a number of trees that you need to cut down, and the only weapon you have is a high-powered antimatter ray that will cut through trees like butter.