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

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

思路:容斥+欧拉函数;

n,m最大公约数为k,那么就转换成找(n/k,m/k)互质的对数;那么这个会想到欧拉函数,但是欧拉函数可以解决,n,m相等的情况,当n,m不等的时候

那么直接用容斥跑(1,min(n,m))在(1,max(m,n))中互质的数的个数,最后再减掉oula[min(n,m)],这是重复的,然后再加上1也就是(1,1)是没重,但在oula[]中减了

  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 }
时间: 2024-08-28 03:56:23

GCD(hdu1695)的相关文章

iOS多线程开发之GCD(下篇)

上篇和中篇讲解了什么是GCD,如何使用GCD,这篇文章将讲解使用GCD中将遇到的死锁问题.有兴趣的朋友可以回顾<iOS多线程开发之GCD(上篇)>和<iOS多线程开发之GCD(中篇)>. 言归正传,我们首先来回顾下死锁,所谓死锁: 是指两个或两个以上的进程(线程)在执行过程中,因争夺资源(如数据源,内存等,变量不是资源)而造成的一种互相等待的现象,若无外部处理作用,它们都将无限等待下去. 死锁形成的原因: 系统资源不足 进程(线程)推进的顺序不恰当: 资源分配不当 死锁形成的条件:

iOS多线程开发之离不开的GCD(上篇)

一.GCD基本概念 GCD 全称Grand Central Dispatch(大中枢队列调度),是一套低层API,提供了?种新的方法来进?并发程序编写.从基本功能上讲,GCD有点像NSOperationQueue,他们都允许程序将任务切分为多个单一任务,然后提交??作队列来并发的或者串?的执行.GCD是C实现,?NSOpertionQueue更底层更高效,并且它不是Cocoa框架的一部分 并发任务会像NSOperationQueue那样基于系统负载来合适地并发进?,而串?行队列同一时间只执行单一

深入理解 GCD(一)

原文出处: Derek Selander   译文出处:nixzhu (@nixzhu)   欢迎分享原创到伯乐头条 虽然 GCD 已经出现过一段时间了,但不是每个人都明了其主要内容.这是可以理解的:并发一直很棘手,而 GCD 是基于 C 的 API ,它们就像一组尖锐的棱角戳进 Objective-C 的平滑世界.我们将分两个部分的教程来深入学习 GCD . 在这两部分的系列中,第一个部分的将解释 GCD 是做什么的,并从许多基本的 GCD 函数中找出几个来展示.在第二部分,你将学到几个 GC

多线程之GCD(一)

Grand Central Dispatch简称(GCD)是由苹果公司开发的技术,是一个非常好的用于多核设备的解决方案.GCD核心在于两个概念: 队列:队列负责管理开发者提交的任务,GCD队列始终以FIFO(先进先出)的方式来处理任务-----但由于处理执行的时间不同,所以先处理的任务并不一定先结束.队列既可以是串行队列,也可以是并发队列,串行队列每次只处理一个任务,必须前一个任务执行完成后,才能执行下一个任务:并发队列则可同时处理多个任务,因此可以多个任务并发处理. 队列底层会维护一个线程池来

【BZOJ】2820: YY的GCD(莫比乌斯)

http://www.lydsy.com/JudgeOnline/problem.php?id=2820 此题非常神! 下文中均默认n<m 首先根据bzoj1101的推理,我们易得对于一个数d使得数对(x,y)=k的个数为: $$\sum_{1<=d<=n'} \mu (d) \times \lfloor \frac{n}{d} \rfloor \times \lfloor \frac{m}{d} \rfloor, 其中n'=\lfoor \frac{n}{k} \rfloor$$ 所以

UVa 1642 - Magical GCD(数论)

链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4517 题意: 输入一个n(n≤100000)个元素的正整数序列,求一个连续子序列,使得该序列中所有元素的最大公约数与序列长度的乘积最大.例如,5个元素的序列30, 60, 20, 20, 20的最优解为{60, 20, 20, 20},乘积为gcd(60,20,20,20)*4=8

Codeforces Round #511 (Div. 2) C. Enlarge GCD (质因数)

题目 题意: 给你n个数a[1]...a[n],可以得到这n个数的最大公约数, 现在要求你在n个数中 尽量少删除数,使得被删之后的数组a的最大公约数比原来的大. 如果要删的数小于n,就输出要删的数的个数, 否则输出 -1 . 思路: 设原来的最大公约数为 g, 然后a[1]...a[n]都除以g ,得到的新的a[1]...a[n],此时它们的最大公约数一定是1 . 设除以g之后的数组a为: 1    2    3     6      8   10  则它们的质因数分别是:  1    2   

iOS多线程GCD(转)

转自:http://www.cnblogs.com/pure/archive/2013/03/31/2977420.html Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main queue,通过dispatch_get_main_queue获取. /*! * @function dispatch_get_main_queue * * @abstract * Returns th

【BZOJ 4305】 4305: 数列的GCD (数论)

4305: 数列的GCD Description 给出一个长度为N的数列{a[n]},1<=a[i]<=M(1<=i<=N). 现在问题是,对于1到M的每个整数d,有多少个不同的数列b[1], b[2], ..., b[N],满足: (1)1<=b[i]<=M(1<=i<=N): (2)gcd(b[1], b[2], ..., b[N])=d: (3)恰好有K个位置i使得a[i]<>b[i](1<=i<=N) 注:gcd(x1,x2,