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个人都谈到了一个关于招 ...
随机推荐
- 电脑盘符为什么从C盘开始?A盘和B盘去哪了?
虽然我们几乎每天都在跟电脑打交道,但是不知道大家有没有过疑惑,为什么电脑盘符是从C盘开始命名呢?有没有A盘和B盘??A盘和B盘去哪了??? 其实,A盘和B盘是真实存在的. 在早期的DOS时代,计算机的 ...
- sed 修改文件
总结 正确的修改进文件命令(替换文件内容):sed -i "s#machangwei#mcw#g" mcw.txt 正确的修改追加进文件命令(追加文件内容):sed -i &quo ...
- VSCode+Maven+Hadoop开发环境搭建
在Maven插件的帮助下,VSCode写Java其实非常方便.这一讲我们介绍如何借助maven用VScode搭建Hadoop开发环境. 1.Java环境安装 首先我们需要搭建好Java开发环境.我们需 ...
- flink01--------1.flink简介 2.flink安装 3. flink提交任务的2种方式 4. 4flink的快速入门 5.source 6 常用算子(keyBy,max/min,maxBy/minBy,connect,union,split+select)
1. flink简介 1.1 什么是flink Apache Flink是一个分布式大数据处理引擎,可以对有限数据流(如离线数据)和无限流数据及逆行有状态计算(不太懂).可以部署在各种集群环境,对各种 ...
- JavaScript小数、百分数的转换
百分数转化为小数 function toPoint(percent){ var str=percent.replace("%",""); str= str/10 ...
- 100个Shell脚本—【脚本6】拷贝目录
[脚本6]拷贝目录 编写shell脚本,把/root/目录下的所有目录(只需要一级)拷贝到/tmp/目录下: 一.脚本 #!/bin/bash cd /root list=(`ls`) for i i ...
- Kotlin 学习(2)
属性和字段 1.声明属性 Kotlin中可以使用var关键字声明可变属性,或者用val关键字声明只读属性,属性的类型在后面,变量名在前面,中间加冒号和空格. public class Address ...
- ClassLoad类加载器与双亲委派模型
1. 类加载器 Class类描述的是整个类的信息,在Class类中提供的方法getName()是根据ClassPath配置的路径来进行类加载的.若类加载的路径为文件.网络等时则必须进行类加载这是就需要 ...
- CentOS 6.5下安装Python+Django+Nginx+uWSGI
1.安装Python31.1先安装zlib库及其他三方库安装uWSGI时需要使用zlib,否则执行python uwsgiconfig.py --build时会报ImportError,就是因为在安装 ...
- Spring实现类私有方法测试通用方案
现实的业务场景中,可能需要对Spring的实现类的私有方法进行测试. 场景描述: 比如XXXService里有 两个函数a.函数b. 而实现类XXXServiceImpl中实现了函数a.函数b,还包含 ...