GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 11177    Accepted Submission(s): 4236

Problem Description
Given 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
The 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.
 
Output
For each test case, print the number of choices. Use the format in the example.
 
Sample Input
2
1 3 1 5 1
1 11014 1 14409 9
 
Sample Output
Case 1: 9
Case 2: 736427
Hint
For the first sample input, all the 9 pairs of numbers are (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5).
 
Source
 
Recommend
wangye   |   We have carefully selected several similar problems for you:  1689 1690 1693 1691 1698 
——————————————————————————————————
题目的意思是给出两个区间,问两个区间有多少对数GCD=k,(1,2)和(2,1)算一种,区间从1开始

思路:先把区间每个数除以k,然后说白了就是求[1,m/k],[1,n/k]有多少对数互质 可以枚举m/k,在n/k中找有多少互质的数,可以先把枚举的m/k质因数的分解,然后利用容斥定理计数,防止重复计数枚举时可以计算i与区间[i,n/k]的互质数;
防止超时分解的质因数可以先打个表
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define LL long long
const int inf=0x3f3f3f3f; vector<LL>p[100010];
int pr[100010];
int a,b,x,y,k;
LL ans; void init()
{
memset(pr,0,sizeof pr);
for(int i=1;i<=100005;i++) p[i].clear();
for(int i=2;i<=100005;i+=2)
p[i].push_back(2);
for(int i=3;i<=100005;i+=2)
{
if(!pr[i])
{
for(int j=i;j<=100005;j+=i)
{
pr[j]=1;
p[j].push_back(i);
} }
}
} void dfs(int no,int pos,int xx,int cnt)
{
if(pos>=p[no].size())
{
if(cnt==0)
return;
if(cnt%2)
{
ans+=y/xx;
ans-=(no-1)/xx;
}
else
{
ans-=y/xx;
ans+=(no-1)/xx;
}
return;
}
dfs(no,pos+1,xx*p[no][pos],cnt+1);
dfs(no,pos+1,xx,cnt);
} int main()
{
init();
int T;
int q=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d%d",&a,&x,&b,&y,&k);
if (k == 0) {
printf("Case %d: 0\n", q++);
continue;
}
x/=k,y/=k;
if(x>y) swap(x,y);
LL ans2=0;
for(int i=1; i<=x; i++)
{
ans=0;
dfs(i,0,1,0);
ans2+=y-i+1-ans;
}
printf("Case %d: %lld\n",q++,ans2);
}
return 0;
}

Hdu1695 GCD 2017-06-27 22:19 30人阅读 评论(0) 收藏的更多相关文章

  1. NYOJ-206 矩形的个数 AC 分类: NYOJ 2013-12-29 22:19 265人阅读 评论(0) 收藏

    这题目是小学奥数题目,方法可以百度到,但是,有个难点就是,数据类型大小不够,如果是1000x1000的矩阵,那么就会超过int的范围,所以,就引进了long long的数据类型 #include< ...

  2. NYOJ-469 擅长排列的小明 II AC 分类: NYOJ 2014-01-02 22:19 159人阅读 评论(0) 收藏

    最初的第一印象是和组合数一个性质的题目.所以用了回溯法,结果,你懂的... #include<stdio.h> #include<math.h> void dfs(int n, ...

  3. Poj2231 Moo Volume 2017-03-11 22:58 30人阅读 评论(0) 收藏

    Moo Volume Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22104   Accepted: 6692 Descr ...

  4. 全方位分析Objcetive-C Runtime 分类: ios技术 2015-03-11 22:29 77人阅读 评论(0) 收藏

    本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 简介 与Runtime交互 ...

  5. Hdu2068 RPG的错排 2017-06-27 15:27 30人阅读 评论(0) 收藏

    RPG的错排 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

  6. android开发之this.finish()的使用 分类: android 学习笔记 2015-07-18 19:05 30人阅读 评论(0) 收藏

    在一个Activity用完之后应该将之finish掉,但是,之前在学校里自己摸索着开发时并没有太注意这个问题,因为activity无论是否finish掉对功能的影响貌似都不是那么明显(这是读书时候的观 ...

  7. java面试和笔试大全 分类: 面试 2015-07-10 22:07 10人阅读 评论(0) 收藏

    2.String是最基本的数据类型吗? 基本数据类型包括byte.int.char.long.float.double.boolean和short. java.lang.String类是final类型 ...

  8. Struts知识问答 分类: 面试 2015-07-10 22:01 4人阅读 评论(0) 收藏

    1. 简述Struts框架的初始化流程. 答案: 对于采用Struts框架的Web应用,在Web应用启动时就会加载并初始化控制器ActionServlet ActionServlet从struts-c ...

  9. Work around by " Due to heavy load, the latest workflow operation has been queued. " 分类: Sharepoint 2015-07-08 00:19 3人阅读 评论(0) 收藏

    I hope most of the users and developers might have come across above note and worried about it. Ther ...

随机推荐

  1. Oracle_PL/SQL(8) 动态sql

    动态sql0.pl/sql块的限制 不能执行ddl操作(create.drop.alter): 不能执行部分dcl操作(grant.revoke). 1.语法动态sql:在执行时才能确定要执行的sql ...

  2. Liunx cal

    1.命令格式: cal [参数][月份][年份] 2.命令功能: 用于查看日历等时间信息,如只有一个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份 3.命令参数: -1 显示一个月的 ...

  3. RNA测序研究现状与发展

    RNA测序研究现状与发展 1 2,584 A+ 所属分类:Transcriptomics   收  藏 通常来说,某一个物种体内所有细胞里含有的DNA都应该是一模一样的,只是因为每一种细胞里所表达的R ...

  4. 在nginx中,禁止IP访问.只可以使用域名访问.

    if ($host ~* "\d+\.\d+\.\d+\.\d+"){ ; } 其实说白了, 就是进行host主机头过滤,使用正则来判断下.

  5. 201621123008 《Java程序设计》第十周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1. 常用异常 结合题集题目7-1回答 1.1 自己以前编写的代码中经常出现 ...

  6. 通过代理上网时,qq等应用程序连网出错

    虽然现在基本上都用无线,有线宽带等,但是有时候还是避免不了通过代理上网时,于是就发生浏览器可以正常浏览网页,qq等应用程序连接出错等问题,上网搜了好长时间,    都没解决问题,后来慢慢琢磨(其实是乱 ...

  7. vue动态路由配置,vue路由传参

    动态路由: 当我们很多个页面或者组件都要被很多次重复利用的时候,我们的路由都指向同一个组件,这时候从不同组件进入一个"共用"的组件,并且还要传参数,渲染不同的数据 这就要用到动态路 ...

  8. mac install brew

    $ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" ...

  9. 微信JSSDK接口previewImage

    <div class="pics"> <img src="http://pic1.ytqmx.com:82/2015/0409/01/15.jpg!96 ...

  10. SVN基本操作 (zz)

    SVN基本操作 分类: LINUX 原文地址:SVN基本操作 作者:tuyer 文章摘要:SVN 基本操作:SVN是什么 Svn是一个离线的代码管理,可以多个人一起修改,然后再将修改的内容提交到Svn ...