HDU1695:GCD(容斥原理+欧拉函数+质因数分解)好题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695
题目解析:
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.
题目又说a==c==1,所以就是求[1,b]与[1,d]中gcd等于k的个数,因为若gcd(x,y)==z,那么gcd(x/z,y/z)==1,又因为不是z的倍数的肯定不是,所以不是z的倍数的可以直接去掉,所以只要将b和d除以k,然后就转化成了求两个范围中互质的对数了。即求[1,b/k],与[1,d/k]中互质的数目,让b<d,又因为 (x=5, y=7) and (x=7, y=5) are considered to be the same.
所以先求[1,b/k]中互质的数目,即phi[1]+phi[2]+phi[3].....+phi[b/k](其中phi[i]为i的欧拉函数值),再从区间[b/k+1,d/k]枚举与区间[1,b/k]中互质的数目。其中求与区间[1,b/k]中互质的数目可以通过容斥原理求得与区间[1,b/k]中不互质的数目,相减便可以求得结果。这题折腾了一中午,一直TLE,代码在后面贴了,之后看大神的博客知道了哪里超时的原因,每个数的质因子可以在打表求欧拉函数的时候顺便求出来,一种哈希的思想,这样就不用在枚举的时候每一个数在求一遍他的质因子了,好题!
代码如下:(608ms)
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <algorithm>
- #include <queue>
- #include <math.h>
- using namespace std;
- typedef __int64 ll;
- ll sum,phi[];
- int cnt[][],f[],a,b,c,d,x;
- void init()
- {
- memset(f,,sizeof(f));
- for(int i=; i<=; i++)
- {
- phi[i]=;
- f[i]=;
- }
- phi[]=;
- for(int i=; i<=; i++)
- {
- if(!phi[i])
- {
- for(ll j=i; j<=; j+=i)
- {
- if(!phi[j]) phi[j]=j;
- phi[j]=phi[j]/i*(i-);
- cnt[j][f[j]++]=i;//算是哈希吧,很精辟啊,这种写法要学习
- }
- }
- phi[i]+=phi[i-];
- }
- }
- ll gcd(ll A,ll B)
- {
- return B==?A:gcd(B,A%B);
- }
- void dfs(ll now,ll num,ll lcm,ll &coun,int key)
- {
- lcm=cnt[key][now]/gcd(cnt[key][now],lcm)*lcm;
- if(num&)
- {
- coun+=b/lcm;
- }
- else
- {
- coun-=b/lcm;
- }
- for(ll i=now+; i<f[key]; i++)
- dfs(i,num+,lcm,coun,key);
- }
- int main()
- {
- int T,K=;
- init();
- scanf("%d",&T);
- while(T--)
- {
- scanf("%d%d%d%d%d",&a,&b,&c,&d,&x);
- if(x == || x > b || x > d)
- {
- printf("Case %d: 0\n",++K);
- continue;
- }
- b/=x;
- d/=x;
- sum=;
- if(b>d) swap(b,d);
- sum+=phi[b];
- for(int i=b+; i<=d; i++)
- {
- ll coun=;
- for(int j=; j<f[i]; j++)
- {
- dfs(j,,cnt[i][j],coun,i);
- }
- sum+=(b-coun);
- }
- printf("Case %d: %I64d\n",++K,sum);
- }
- return ;
- }
TLE的:(TLE了一中午 3000ms++)
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <algorithm>
- #include <queue>
- #include <math.h>
- using namespace std;
- typedef __int64 ll;
- ll a,b,c,d,x,sum,top,cnt[],we;//********
- int phi[],su[],prime[];
- void init()
- {
- we=;
- prime[we++]=;
- su[]=su[]=;
- su[]=;
- for(int i=; i<; i++)
- if(i%==) su[i]=;
- else su[i]=;
- double t=sqrt(*1.0);
- for(ll i=; i<=t; i++)
- {
- if(su[i])
- {
- for(ll j=i*i; j<; j=j+i)
- {
- su[j]=;
- }
- }
- }
- for(ll i=; i<=; i++)
- {
- if(su[i]) prime[we++]=i;
- }
- memset(phi,,sizeof(phi));
- phi[]=;
- for(ll i=; i<=; i++)
- {
- if(!phi[i])
- {
- for(ll j=i; j<=; j+=i)
- {
- if(!phi[j]) phi[j]=j;
- phi[j]=phi[j]/i*(i-);
- }
- }
- }
- }
- ll gcd(ll A,ll B)
- {
- return B==?A:gcd(B,A%B);
- }
- void dfs(ll now,ll num,ll lcm,ll &coun)
- {
- lcm=cnt[now]/gcd(cnt[now],lcm)*lcm;
- if(num&)
- {
- coun+=b/lcm;
- //printf("hsum======%I64d\n",sum);
- }
- else
- {
- coun-=b/lcm;
- }
- for(ll i=now+; i<top; i++)
- dfs(i,num+,lcm,coun);
- }
- void cal(ll key,ll &coun)
- {
- top=;
- ll KK=;
- for(ll i=prime[]; i<=key; i=prime[++KK])
- {
- if(key%i==)
- {
- cnt[top++]=i;
- key/=i;
- while(key%i==)
- key/=i;
- }
- }
- if(key!=)
- {
- cnt[top++]=key;
- }
- for(ll i=; i<top; i++)
- {
- dfs(i,,cnt[i],coun);
- }
- }
- int main()
- {
- int T,K=;
- init();
- scanf("%d",&T);
- while(T--)
- {
- scanf("%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&x);
- if(x == || x > b || x > d)
- {
- printf("Case %d: 0\n",++K);
- continue;
- }
- b/=x;
- d/=x;
- sum=;
- if(b>d) swap(b,d);
- //cout<<"b=="<<b<<" "<<"d=="<<d<<endl;
- for(int i=; i<=b; i++)
- {
- sum+=phi[i];
- }
- //cout<<"sumsss=="<<sum<<endl;
- for(ll i=b+; i<=d; i++)
- {
- if(su[i])
- {
- sum+=b;
- continue;
- }
- ll coun=;
- cal(i,coun);
- sum+=(b-coun);
- }
- printf("Case %d: %I64d\n",++K,sum);
- }
- return ;
- }
HDU1695:GCD(容斥原理+欧拉函数+质因数分解)好题的更多相关文章
- T^TOJ - 1251 - 。◕‿◕。TMD - 欧拉函数 - 质因数分解
http://www.fjutacm.com/Problem.jsp?pid=1251 想了很久,一开始居然还直接枚举因子d,计算重复了. 首先你要找与n的最大公因子大于m的x的个数. \[\sum\ ...
- HDU1695 GCD (欧拉函数+容斥原理)
F - GCD Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- HDU 1695 GCD (容斥原理+欧拉函数)
题目链接 题意 : 从[a,b]中找一个x,[c,d]中找一个y,要求GCD(x,y)= k.求满足这样条件的(x,y)的对数.(3,5)和(5,3)视为一组样例 . 思路 :要求满足GCD(x,y) ...
- HDU 1695 GCD(欧拉函数+容斥原理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...
- hdu 1695 GCD (欧拉函数、容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- HDU 1695 GCD (欧拉函数+容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 1695 GCD(欧拉函数+容斥)
Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD( ...
- HDU2588:GCD(欧拉函数的应用)
题目链接:传送门 题目需求:Given integers N and M, how many integer X satisfies 1<=X<=N and (X,N)>=M.(2& ...
- hdu2588 GCD (欧拉函数)
GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数. (文末有题) 知 ...
随机推荐
- 【转自IT虾米网:www.itxm.net】外部应用和drools-wb6.1集成解决方案
一.手把手教你集成外部应用和drools workbench6.1 1. 首先按照官方文档安装workbench ,我用的是最完整版的jbpm6-console的平台系统,里面既包含j ...
- Storm-源码分析-Streaming Grouping (backtype.storm.daemon.executor)
executor在发送outbounding message的时候, 需要决定发送到next component的哪些tasks 这里就需要用到streaming grouping, 1. mk- ...
- [java] Unsupported major.minor version 51.0 错误解决方案
jdk1.6工程中使用外部jar包中类出现:Unsupported major.minor version 51.0原因分析:出现上述错误是因为:外部jar包使用jdk1.7(jdk7)编译,而使用此 ...
- 虚幻4 - ARPG实战教程(第一季)
在广受欢迎的的<虚幻4高速开发入门>视频教程之后.我收到了许多的反馈,当中大量的同学想要一个实战类的教程.于是,我花了一段时间准备之后,推出了新的一系列实战教程. 希望以深入浅出的方式.解 ...
- 墨卡托投影, GPS 坐标转像素, GPS 坐标转距离
Before: 1. 研究的需要, 在 google map 上爬取了一些的静态卫星地图图片,每张图片的像素为 256*256 2. 通过 photshop 将这些地图碎片手动拼成了地图, 地图只是覆 ...
- PyQt4文件对话框QFileDialog
文件对话框允许用户选择文件或文件夹,被选择的文件可进行读或写操作. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 im ...
- 【Mysql】 case ... when ... 用法
sql语句查询时给某个空字段赋值 SELECT CASE WHEN field= '' THEN ' WHEN fieldIS NULL THEN ' ELSE field END FROM tabl ...
- 1.Math函数对象
// 属性 Math.E //自然对数的底数(2.718281828459045) Math.PI //圆周率(3.141592653589793) Math.LN2 //2的自然对数(0.69314 ...
- centos6.5安装sendmail
1.下载安装sendEmail(下载绿色版,解压可直接使用) wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1. ...
- mysql客户端不能插入中文字符
问题:输入中文报错:Incorrect string value 步骤: 1.查看MySQL编码设置 show variables like '%character%'; 2.重新设置编码(注意:ut ...