1144 - Ray Gun
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 }

1144 - Ray Gun的更多相关文章

  1. php获取汉字的拼音 拼音首字母

    /***获取汉字的拼音*/function pinyin($s, $isfirst = false) { static $pinyins; $s = trim($s); $len = strlen($ ...

  2. UvaLA 3938 "Ray, Pass me the dishes!"

                            "Ray, Pass me the dishes!" Time Limit: 3000MS   Memory Limit: Unkn ...

  3. POJ 1144

    http://poj.org/problem?id=1144 题意:给你一些点,某些点直接有边,并且是无向边,求有多少个点是割点 割点:就是在图中,去掉一个点,无向图会构成多个子图,这就是割点 Tar ...

  4. ray与triangle/quad求交二三事

    引擎中,ray与quad求交,算法未细看,但有求解二次方程,不解.ray与triangle求交,使用的是97年经典算法,仔细看过论文,多谢小武同学指点,用到了克拉默法则求解线性方程组.想模仿该方法,做 ...

  5. OpenCascade Ray Tracing Rendering

    OpenCascade Ray Tracing Rendering eryar@163.com 摘要Abstract:OpenCascade6.7.0中引入了光线跟踪算法的实现.使用光线跟踪算法可实现 ...

  6. 【Unity3d】Ray射线初探-射线的原理及用法

    http://www.xiaobao1993.com/231.html 射线是一个无穷的线,开始于origin并沿着direction方向. 当射线碰到物体后.它就会停止发射. 在屏幕中拉一个CUBE ...

  7. ural 1144. The Emperor's Riddle

    1144. The Emperor's Riddle Time limit: 1.0 secondMemory limit: 4 MB Background In the olden times th ...

  8. Load Mental Ray in Maya 2015

    In Maya 2015, we usually use mental ray to render our model, some new users may not see the mental r ...

  9. 2013MPD上海6.22 PM 陆宏杰:通往卓越管理的阶梯 & 6.23AM Ray Zhang 产品创新管理的十八般武艺

    MPD2天的内容,参加了5个课程,其中2个是管理的,分别是陆宏杰老师的<通往卓越管理的阶梯>和Ray Zhang大师的<产品创新管理的十八般武艺>.他们2个人都谈到了一个关于招 ...

随机推荐

  1. A Child's History of England.33

    To strengthen his power, the King with great ceremony betrothed his eldest daughter Matilda, then a ...

  2. Java【常用的日期操作】

    目录 1.设置时间 2.获取年月日时分秒 3.通过运算获取时间 4.和Date类转换 5.格式化时间 6.新功能LocalDate:当前日期格式化 7.示例 java.util.Calendar 类是 ...

  3. Linux学习 - 文件系统常用命令

    一.文件系统查看命令df df [选项] [挂载点] -a 查看所有文件系统信息,包括特殊文件系统 -h 使用习惯单位显示容量 -T 显示文件系统类型 -m 以MB为单位显示容量 -k 以KB为单位显 ...

  4. vue引入d3

    单页面使用 cnpm install d3 --save-dev 指定版本安装 cnpm install d3@6.3.1 -S <script> import * as d3 from ...

  5. web前段canvasjs图表制作一

    关于web图表制作的方法有很多种,大家可以去网上这里我就介绍我经常使用的一种方法CanvasJS,这也是一款非常容易掌握并且好用的一种方法. 首先可以去网上下载canvasjs.js插件.下载路径:h ...

  6. 接下来一段时间会对大家进行网络通信的魔鬼训练-理解socket

    引子 下一篇标题是<深入理解MQ生产端的底层通信过程>,建议文章读完之前.或者读完之后,再读一遍我之前写的<RabbitMQ设计原理解析>,结合理解一下. 我大学时流行过一个韩 ...

  7. 对于React各种状态管理器的解读

    首先我们要先知道什么是状态管理器,这玩意是干啥的? 当我们在多个页面中使用到了相同的属性时就可以用到状态管理器,将这些状态存到外部的一个单独的文件中,不管在什么时候想使用都可以很方便的获取. reac ...

  8. git push大文件失败(write error: Broken pipe)完美解决

    问题 在使用git push推送大文件(超过了100MB)到GitHub远程仓库时提示异常,异常信息如下: fatal: sha1 file '<stdout>' write error: ...

  9. 9、Redis五大数据类型---有序集合Zset(sorted set)

    一.简介 zset与set异同 相同之处: 都是没有重复元素的字符串集合 不同之处: 有序集合zset的每个成员都关联了一个评分(score),这个评分(score)被用来按照从最低分到最高分的方式排 ...

  10. 4、Linux下安装tomcat

    Linux系统下安装tomcat 一.安装JDK 安装Tomcat之前需要安装JDk,安装JDk请参考:JDK安装. 二.Linux安装Tomcat 1.官网上下载Tomcat       Apach ...