HDU1659-GCD-容斥原理
从1-a和1-b种选两个数xy,计算出令gcd(x,y)=k的xy的对数。
对于每一个i∈[1,b]使用solve(i,n)函数解决有几个j∈[1,n]使gcd(x,y)=k。然后累加solve(i,n)-solve(i,i)即可,注意边界情况。
solve函数则使用容斥原理。
#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std; const int maxn = ;
int T,a,b,c,d,k;
long long ans;
int prime[maxn]; void init()
{
for(int i=;i<=maxn;i++)
{
if(!prime[i]) prime[++prime[]] = i;
for(int j=;j<=prime[]&&prime[j]<=maxn/i;j++)
{
prime[prime[j]*i] = ;
if(i%prime[j]==) break;
}
}
} int factor[];
int fatCnt; int getFactors(int x)
{
fatCnt = ;
int tmp = x;
for(int i=;prime[i]<=tmp/prime[i];i++)
{
if(tmp%prime[i] == )
{
factor[fatCnt] = prime[i];
while(tmp%prime[i] == )
{
tmp/=prime[i];
//factor[fatCnt] *= prime[i];
}
fatCnt++;
}
}
if(tmp != )
{
factor[fatCnt++] = tmp;
}
return fatCnt;
} long long solve(int x,int n)
{
int np = getFactors(x);
int cnt,lcm;
long long res = n/k; //printf("x=%d n=%d np=%d \n",x,n,np);
for(int i=;i<(<<np);i++)
{
cnt=;lcm=;
int flag = ;
for(int j=;j<fatCnt;j++)
{
if((<<j) & i)
{
lcm *= factor[j];
cnt++;
}
}
lcm *= k;
cnt++; if(cnt&)
res += n/lcm;
else
res -= n/lcm;
}
//printf("res=%d\n",res);
return res;
} int main()
{
scanf("%d",&T);
init();
for(int kase=;kase<=T;kase++)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
printf("Case %d: ",kase);
if(k == ) {printf("0\n");continue;}
if(d > b) swap(d,b); ans = ;
for(int i=c;i<=d;i++) if(i%k == )
{
ans += (solve(i/k,b)-solve(i/k,i));
if(i == k) ans++;
}
printf("%lld\n",ans);
}
}
HDU1659-GCD-容斥原理的更多相关文章
- 51nod 1678 lyk与gcd | 容斥原理
51nod 200题辣ψ(`∇´)ψ !庆祝! 51nod 1678 lyk与gcd | 容斥原理 题面 这天,lyk又和gcd杠上了. 它拥有一个n个数的数列,它想实现两种操作. 1:将 ai 改为 ...
- HDU 1695 GCD#容斥原理
http://acm.hdu.edu.cn/showproblem.php?pid=1695 翻译题目:给五个数a,b,c,d,k,其中恒a=c=1,x∈[a,b],y∈[c,d],求有多少组(x,y ...
- hdu1695 GCD 容斥原理
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) ...
- SCUT - 157 - CC和他的GCD - 容斥原理
https://scut.online/p/157 鉴于多年(都没几个月)搞数论的经验,这种时候枚举g肯定是对的. 那么肯定是要莫比乌斯函数作为因子,因为很显然? 但是为什么要搞个负的呢?其实是因为这 ...
- HDU 1695 GCD (欧拉函数+容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 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) ...
- UVa 1393 (容斥原理、GCD) Highways
题意: 给出一个n行m列的点阵,求共有多少条非水平非竖直线至少经过其中两点. 分析: 首先说紫书上的思路,编程较简单且容易理解.由于对称性,所以只统计“\”这种线型的,最后乘2即是答案. 枚举斜线包围 ...
- HDU 1695 GCD(欧拉函数+容斥原理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...
- 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( ...
随机推荐
- COMCMS_CORE 起步篇,如何运行和部署
前言:关于最近开源后,不少朋友问,怎么我下载下来,运行不了.或者怎么没有左边菜单.货不对板?还是我吃了数据? 感言:开源不容易,更不容易的是,明明毫无保留,还这么大误会,真是泪奔..... 好了.步入 ...
- ZooKeeper: 简介, 配置及运维指南
1. 概览 ZooKeeper是一个供其它分布式应用程序使用的软件, 它为其它分布式应用程序提供所谓的协调服务. 所谓的协调服务, 是指ZooKeeper的如下能力 naming 命名 configu ...
- Java 控制语句:循环、条件判断
基础很重要,基础很重要,基础很重要.重要的事情说三遍,. 程序设计中的控制语句主要有三种:顺序.分支和循环.我们每天写的代码,除了业务相关,里面会包含大量的控制语句.但是控制语句的基本使用,是否有些坑 ...
- Leetcode-645 Set Mismatch
The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of ...
- Python_装饰器精讲_33
from functools import wraps def wrapper(func): #func = holiday @wraps(func) def inner(*args,**kwargs ...
- BFC 原理
BFC:Block-level box + Forating + Context; ------->块元素 决定其子元素如何定位, ...
- CSS颜色代码 颜色值 颜色名字大全
颜色值 CSS 颜色使用组合了红绿蓝颜色值 (RGB) 的十六进制 (hex) 表示法进行定义.对光源进行设置的最低值可以是 0(十六进制 00).最高值是 255(十六进制 FF).从 0 到 25 ...
- 解决sqoop连接mysq错误
一.问题描述 1.由于当前集群没有配置Zookeeper.hcatalog.accumlo,因此应该在sqoop的配置文件中注释掉判断Zookeeper.hcatalog.accumlo路径是否正确的 ...
- spark、standalone集群 (1)
1.配置 spark/apache/org 下载解压, 安装jdk1.8 2.准备服务器 3.设置hostname 4.关闭防火墙 开启: service iptables start 关闭: ser ...
- day 7-20 视图,触发器,事务
一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...