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. Redis | 第8章 发布订阅与事务《Redis设计与实现》

    目录 前言 1. 发布订阅 1.1 频道的订阅与退订 1.2 模式的订阅与退订 1.3 发送消息 1.4 查看订阅消息 2. 事务 2.1 事务的实现 2.2 WATCH 命令的实现 2.3 事务的 ...

  2. 学习 DDD - 通用语言的模式

    大家好,我是霸戈,这周学习了一些关于领域驱动设计的知识 ,对比较深刻的地方做了不少笔记,分享给大家. 在日常需求讨论的时候,经常会碰到一个需求会议开了一个多小时还没有达成共识.作为业务方(领域专家)明 ...

  3. Android 获取html中指定标签

    有时我们并不需要全部的html页面,而只是需要其中的部分标签,我们可以通过jsoup来完成这一操作. 官网:https://jsoup.org/ 1 Document document = Jsoup ...

  4. 日常Java 2021/11/9

    线程的优先级 每一个Java线程都有一个优先级,这样有助于操作系统确定线程的调度顺序.Java线程的优先级是一个整数,其取值范围是1(Thread.MIN_PRIORITY ) -10 (Thread ...

  5. 自然语言式parsing

    got NUM(1) Is NUM(1) an expr? Is NUM(1) a term? Is NUM(1) a number? is_term got -(-) -(-) was back i ...

  6. A Child's History of England.19

    The King was at first as blind and stubborn as kings usually have been whensoever [每当] they have bee ...

  7. acute

    In Euclidean geometry, an angle is the figure formed by two rays, called the sides of the angle, sha ...

  8. [php反序列化] CVE-2020-15148(Yii2 反序列化漏洞) 漏洞复现

    漏洞影响范围 Yii2 < 2.0.38 环境搭建 Yii2.0.37 漏洞分析 首先定位到漏洞起始点 为什么是这儿?我们该怎么发现是某个类的某个函数?为什么不是其他函数? 一般是__destr ...

  9. STL学习笔记1

    STL六大部件 容器.分配器.算法.迭代器.适配器.仿函数 他们的关系如下

  10. Oracle—表、约束、索引、表空间、分区、序列、统计信息

    表.约束.索引.表空间.分区.序列.统计信息 一.表及其操作 1.创建表 create table 表名 ( 字段名1 字段类型 默认值 是否为空 , 字段名2 字段类型 默认值 是否为空, 字段名3 ...