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个人都谈到了一个关于招 ...
随机推荐
- MybatisPlus入门程序
参考资料:MybatisPlus官网 环境搭建 创建数据库 CREATE DATABASE `mybatisplus` USE `mybatisplus` CREATE TABLE `user ...
- linux vi和vim编辑器
所有的Linux系统都会内建vi文本编辑器,vim具有程序编辑的能力,可以看作是vi的增强版本 三种常见模式 正常模式 以vim打开一个文档直接进入的模式,快捷键可以使用. 1.这个模式可以使用上下左 ...
- 日常Java 2021/10/5
java 异常处理 Throwable中包括Error 和Exception,Exception包括IOException和RuntimeException 抛出异常 1.异常运算条件 Arithme ...
- LVM磁盘创建与扩容
以虚拟机为例 1.在虚拟机上添加新磁盘,点击虚拟机→设置->添加,最后如下图. 2.进入系统fdisk -l,查看当前磁盘信息 [root@master shell]# fdisk -l Dis ...
- CVTE第二次笔试
选择瞎答得,直接编程题目 1. 使用递归将字符串中的数字去并按顺序打印 输入例 adfsafsfs123123eogie09789 输出例 123123 09789 #include<iost ...
- 顺序栈(C++)
栈的定义为只允许在表的末端进行插入和删除的线性表.简而言之就是先进后出的线性表. 插入和删除的一端被称呼为栈顶(top),而不允许插入删除的一端被称为栈底(bottom).无元素时的栈即为空栈. 使用 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(四)-介绍库函数,获取一些SD卡的信息
其他链接 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 ...
- jquery总结和注意事项
1.关于页面元素的引用通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用dom ...
- 页面屏蔽backspace键
1 //页面加载完成 2 $(document).ready(function(){ 3 //禁止退格键 作用于Firefox.Opera 4 document.onkeypress = banBac ...
- Spring Cloud使用
一.创建提供者工程01-provider-8081 (1) 创建工程 创建一个Spring Initializr工程,并命名为01-provider-8081.导入Lombok.Web.JPA及MyS ...