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 |
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的更多相关文章
- php获取汉字的拼音 拼音首字母
/***获取汉字的拼音*/function pinyin($s, $isfirst = false) { static $pinyins; $s = trim($s); $len = strlen($ ...
- UvaLA 3938 "Ray, Pass me the dishes!"
"Ray, Pass me the dishes!" Time Limit: 3000MS Memory Limit: Unkn ...
- POJ 1144
http://poj.org/problem?id=1144 题意:给你一些点,某些点直接有边,并且是无向边,求有多少个点是割点 割点:就是在图中,去掉一个点,无向图会构成多个子图,这就是割点 Tar ...
- ray与triangle/quad求交二三事
引擎中,ray与quad求交,算法未细看,但有求解二次方程,不解.ray与triangle求交,使用的是97年经典算法,仔细看过论文,多谢小武同学指点,用到了克拉默法则求解线性方程组.想模仿该方法,做 ...
- OpenCascade Ray Tracing Rendering
OpenCascade Ray Tracing Rendering eryar@163.com 摘要Abstract:OpenCascade6.7.0中引入了光线跟踪算法的实现.使用光线跟踪算法可实现 ...
- 【Unity3d】Ray射线初探-射线的原理及用法
http://www.xiaobao1993.com/231.html 射线是一个无穷的线,开始于origin并沿着direction方向. 当射线碰到物体后.它就会停止发射. 在屏幕中拉一个CUBE ...
- 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 ...
- 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 ...
- 2013MPD上海6.22 PM 陆宏杰:通往卓越管理的阶梯 & 6.23AM Ray Zhang 产品创新管理的十八般武艺
MPD2天的内容,参加了5个课程,其中2个是管理的,分别是陆宏杰老师的<通往卓越管理的阶梯>和Ray Zhang大师的<产品创新管理的十八般武艺>.他们2个人都谈到了一个关于招 ...
随机推荐
- makefile高级用法
在某些文件里面找.o find ./ -name "*.o" -e ls- R 看文件目录下面的文件 嵌套 和 VPATH [1]VPATH的用法 (1)VPATH:虚路径 1) ...
- 虚拟节点轻松应对 LOL S11 百万并发流量——腾竞体育的弹性容器实践
作者 刘如梦,腾竞体育研发工程师,擅长高并发.微服务治理.DevOps,主要负责电竞服务平台架构设计和基础设施建设. 詹雪娇,腾讯云弹性容器服务EKS产品经理,主要负责 EKS 虚拟节点.容器实例相关 ...
- Can namespaces be nested in C++?
In C++, namespaces can be nested, and resolution of namespace variables is hierarchical. For example ...
- Can static functions be virtual in C++?
In C++, a static member function of a class cannot be virtual. For example, below program gives comp ...
- Linux基础命令---mput上传ftp文件
mput 使用lftp登录ftp服务器之后,可以使用put指令将文件上传到服务器.mput指令可以使用通配符,而put指令则不可以. 1.语法 mput [-c] [-d] [-a] ...
- 【Python】【Algorithm】排序
冒泡排序 dic = [12, 45, 22, 6551, 74, 155, 6522, 1, 386, 15, 369, 15, 128, 123, ] for j in range(1, len( ...
- 【Linux】【Services】【Package】yum
Linux程序包管理(2) CentOS: yum, dnf URL: ftp://172.16.0.1/pub/ YUM: yellow dog, Yellow ...
- apply 和 call 的区别
相同点: 都能够改变方法的执行上下文(执行环境),将一个对象的方法交给另一个对象来执行,并且是立即执行 不同点: call方法从第二个参数开始可以接收任意个参数,每个参数会映射到相应位置的func的参 ...
- springMVC WebApplicationInitializer 替代web.xml 配置Servlet 之原理
Servlet 3.0之前 ,xml 配置 在过去搭建spring + springMCV ,首先第一步要做的是什么 ,就是要配置web.xml 文件 ,把springMVC 中的Servlet 加 ...
- <转>C/S架构分析
系统架构师-基础到企业应用架构-客户端/服务器 开篇 上篇,我们介绍了,单机软件的架构,其实不管什么软件系统,都是为了解决实际中的一些问题,软件上为了更好的解决实际的问题才会产生,那么对于单机软 件的 ...