GCD(hdu1695)
GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9696 Accepted Submission(s): 3623
5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that
GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y.
Since the number of choices may be very large, you're only required to
output the total number of different number pairs.
Please notice that, (x=5, y=7) and (x=7, y=5) are considered to be the same.
Yoiu can assume that a = c = 1 in all test cases.
input consists of several test cases. The first line of the input is
the number of the cases. There are no more than 3,000 cases.
Each
case contains five integers: a, b, c, d, k, 0 < a <= b <=
100,000, 0 < c <= d <= 100,000, 0 <= k <= 100,000, as
described above.
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[100005];
12 int ans[100005];
13 int flag[100005];
14 int fen[100];
15 int d[100005];
16 LL oula[100005];
17 int slove(int n,int m);
18 int main(void)
19 {
20 int i,j,k;
21 fill(ans,ans+100005,1);
22 fill(d,d+100005,1);
23 int c=0;
24 for(i=0; i<=100000; i++)oula[i]=i;
25 for(i=2; i<=100000; i++)
26 {
27 if(!prime[i])
28 {
29 for(j=2; (i*j)<=100000; j++)
30 {
31 prime[i*j]=true;
32 ans[i*j]*=i;
33 d[i*j]=i;
34 }
35 }
36 }
37 oula[0];
38 oula[1]=1;
39 for(i=2; i<=100000; i++)
40 {
41 if(!prime[i])
42 {
43 ans[i]*=i;
44 d[i]=i;
45 for(j=1; (LL)i*(LL)j<=100000; j++)
46 {
47 oula[i*j]/=i;
48 oula[i*j]*=(i-1);
49 }
50 }
51 }
52 int s;
53 scanf("%d",&k);
54 LL sum=0;
55 int n,m;
56 for(i=2; i<=100000; i++)oula[i]+=oula[i-1];
57 for(s=1; s<=k; s++)
58 {
59 sum=0;
60 int xx,yy,vv;
61 memset(flag,-1,sizeof(flag));
62 scanf("%d %d %d %d %d",&xx,&n,&yy,&m,&vv);
63 if(vv>n||vv>m||vv==0)
64 {
65 printf("Case %d: ",s);
66 printf("0\n");
67 }
68 else
69 {
70 if(n>m)
71 {
72 swap(n,m);
73 }
74 n/=vv;
75 m/=vv;
76 sum=0;
77 for(i=1; i<=n; i++)
78 {
79 if(flag[ans[i]]!=-1)
80 {
81 sum+=flag[ans[i]];
82 }
83 else
84 {
85 flag[ans[i]]=slove(i,m);
86 sum+=flag[ans[i]];
87 }
88 }
89 //printf("%lld %lld\n",oula[5],sum);
90 printf("Case %d: %lld\n",s,sum-oula[min(n,m)]+1);
91 }
92 }
93 return 0;
94 }
95 int slove(int n,int m)
96 {
97 int i,j,k;
98 int nn=n;
99 int cnt=0;
100 while(n>1)
101 {
102 fen[cnt++]=d[n];
103 n/=d[n];
104 }
105 int cc=1<<cnt;
106 LL sum=0;
107 int sum1=0;
108 for(i=1; i<cc; i++)
109 {
110 int ck=0;
111 int ak=1;
112 for(j=0; j<cnt; j++)
113 {
114 if(i&(1<<j))
115 {
116 ak*=fen[j];
117 ck++;
118 }
119 }
120 if(ck%2)
121 {
122 sum+=m/ak;
123 }
124 else sum-=m/ak;
125 }
126 return m-sum;
127 }
GCD(hdu1695)的更多相关文章
- iOS多线程开发之GCD(中篇)
前文回顾: 上篇博客讲到GCD的实现是由队列和任务两部分组成,其中获取队列的方式有两种,第一种是通过GCD的API的dispatch_queue_create函数生成Dispatch Queue:第二 ...
- 求gcd(最大公因数),lcm(最小公倍数)模板
gcd(最大公因数),lcm(最小公倍数) #include<iostream> using namespace std; int gcd(int a,int b)//辗转相除法(欧几里德 ...
- ios专题 - GCD(1)
什么是GCD? Grand Central Dispatch或者GCD,是一套低层API,提供了一种新的方法来进行并发程序编写.从基本功能上讲,GCD有点像 NSOperationQueue,他们都允 ...
- iOS多线程开发之离不开的GCD(上篇)
一.GCD基本概念 GCD 全称Grand Central Dispatch(大中枢队列调度),是一套低层API,提供了⼀种新的方法来进⾏并发程序编写.从基本功能上讲,GCD有点像NSOperatio ...
- 深入GCD(一): 基本概念和Dispatch Queue
什么是GCD?Grand Central Dispatch或者GCD,是一套低层API,提供了一种新的方法来进行并发程序编写.从基本功能上讲,GCD有点像NSOperationQueue,他们都允许程 ...
- 【BZOJ】2820: YY的GCD(莫比乌斯)
http://www.lydsy.com/JudgeOnline/problem.php?id=2820 此题非常神! 下文中均默认n<m 首先根据bzoj1101的推理,我们易得对于一个数d使 ...
- ios专题 - GCD(2)
何为Dispatch Sources 简单来说,dispatch source是一个监视某些类型事件的对象.当这些事件发生时,它自动将一个block放入一个dispatch queue的执行例程中. ...
- 多线程编程 - GCD(转)
原文:http://blog.csdn.net/q199109106q/article/details/8566300 一.简介 在iOS所有实现多线程的方案中,GCD应该是最有魅力的,因为GCD本身 ...
- 2018.06.29 NOIP模拟 Gcd(容斥原理)
Gcd 题目背景 SOURCE:NOIP2015-SHY-2 题目描述 给出n个正整数,放入数组 a 里. 问有多少组方案,使得我从 n 个数里取出一个子集,这个子集的 gcd 不为 1 ,然后我再从 ...
随机推荐
- opencv学习(三)——绘图功能
绘图功能 我们将学习以下函数:cv.line(),cv.circle(),cv.rectangle(),cv.ellipse(),cv.putText()等. 在这些功能中,有一些相同的参数: img ...
- .NET Core基础篇之:集成Swagger文档与自定义Swagger UI
Swagger大家都不陌生,Swagger (OpenAPI) 是一个与编程语言无关的接口规范,用于描述项目中的 REST API.它的出现主要是节约了开发人员编写接口文档的时间,可以根据项目中的注释 ...
- A Child's History of England.50
'Knave [man without honor]!' said King Richard. 'What have I done to thee [you] that thou [you] shou ...
- C++之error: cannot bind non-const lvalue reference of type ‘myString&’ to an rvalue of type ‘myString’
先看代码(不想看代码可以直接看代码后的问题描述) //header.h #ifndef _HEADER_H #define _HEADER_H #define defaultSize 128 #inc ...
- Android 开源框架Universal-Image-Loader加载https图片
解决方案就是 需要 android https HttpsURLConnection 这个类忽略证书 1,找到 Universal-Image-Loader的library依赖包下面com.nostr ...
- proguard 混淆工具的用法 (适用于初学者参考)
一. ProGuard简介 附:proGuard官网 因为Java代码是非常容易反编码的,况且Android开发的应用程序是用Java代码写的,为了很好的保护Java源代码,我们需要对编译好后的cla ...
- mysqldump冷备份
数据库备份的重要性 提高系统的高可用性和灾难可恢复性,在数据库系统崩溃时,没有数据备份就没法找到数据. 使用数据库备份还原数据库,是数据库崩溃时提供数据恢复最小代价的最优方案. 没有数据库就没有一切, ...
- MyBatis(3):优化MyBatis配置文件
一.连接数据库的配置单独放在一个properties文件中 1,创建一个database.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql: ...
- ssm-book 整合案例
一:环境及要求 环境: IDEA最新版 MySQL 5.7.19 Tomcat 9 Maven 3.6 要求: 需要掌握 MyBatis:Spring:SpringMVC:MySQL数据库 ...
- 【Spark】【RDD】初次学习RDD 笔记 汇总
RDD Author:萌狼蓝天 [哔哩哔哩]萌狼蓝天 [博客]https://mllt.cc [博客园]萌狼蓝天 - 博客园 [微信公众号]mllt9920 [学习交流QQ群]238948804 目录 ...