CodeForces 992B Nastya Studies Informatics + Hankson的趣味题(gcd、lcm)
http://codeforces.com/problemset/problem/992/B


题意:
给你区间[l,r]和x,y 问你区间中有多少个数对 (a,b) 使得 gcd(a,b)=x lcm(a,b)=y ,如果a,b交换位置就是不同的数对
思路:
根据lcm(最小公倍数) 的定义 y=a*b/x; 也就是说 x∗y=a∗b ;
那么 ,我们发现a,b一定为y的因数,所以我们枚举y的每个因子就可以,我们只要用log(y)的复杂度暴力算每一个因数就可以 ,
然后对于每个因子当做a, b=x*y/a; 然后判断a,b是否在区间内,gcd(a,b)是否为x,(注意要判断是否等于b)
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
//const double PI=acos(-1);
#define Bug cout<<"---------------------"<<endl
const int maxm=1e6+;
const int maxn=1e5+;
using namespace std; LL gcd(LL a,LL b)
{
return b? gcd(b,a%b):a;
} int main()
{
int l,r,x,y;
scanf("%d %d %d %d",&l,&r,&x,&y);
int ans=;
for(LL i=;i*i<=y;i++)//第一个因子
{
if(y%i==)
{
LL j=x*(y/i);
if(i>=l&&i<=r&&j>=l&&j<=r&&gcd(i,j)==x)
ans++;
LL ii=y/i;//对应的另一个因子
if(i!=ii)
{
LL jj=x*(y/ii);
if(ii>=l&&ii<=r&&jj>=l&&jj<=r&&gcd(ii,jj)==x)
ans++;
}
}
}
printf("%d\n",ans);
return ;
}
Hankson的趣味题
Description
Input
接下来的n 行每 行一组输入数据,为四个正整数a0,a1,b0,b1,每两个整数之间用一个空格隔开。输入 数据保证a0 能被a1 整除,b1 能被b0 整除。
Output
对于每组数据:若不存在这样的 x,请输出0; 若存在这样的 x,请输出满足条件的x 的个数;
Sample Input
2
41 1 96 288
95 1 37 1776
Sample Output
6
2
HINT
样例说明
第一组输入数据,x 可以是9、18、36、72、144、288,共有6 个。
第二组输入数据,x 可以是48、1776,共有2 个。
数据规模和约定
对于 50%的数据,保证有1≤a0,a1,b0,b1≤10000 且n≤100。
对于 100%的数据,保证有1≤a0,a1,b0,b1≤2,000,000,000 且n≤2000。


题解:
https://www.cnblogs.com/five20/p/8434085.html
代码如下(无优化):
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
//const double PI=acos(-1);
#define Bug cout<<"---------------------"<<endl
const int maxn=1e5+;
using namespace std; LL gcd(LL a,LL b)
{
if(b==) return a;
else return gcd(b,a%b);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
LL a,b,c,d;
scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
if(a%b||d%c||d%b)
printf("0\n");
else
{
int num=;
for(int x=;x*x<=d;x++)
{
if(d%x==)
{
if(x%b==&&gcd(x/b,a/b)==&&gcd(d/x,d/c)==) num++;
int y=d/x;
if(x==y) continue;
if(y%b==&&gcd(y/b,a/b)==&&gcd(d/y,d/c)==) num++;
}
}
printf("%d\n",num);
}
}
return ;
}
CodeForces 992B Nastya Studies Informatics + Hankson的趣味题(gcd、lcm)的更多相关文章
- Nastya Studies Informatics CodeForces - 992B (大整数)
B. Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input ...
- Nastya Studies Informatics
Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes in ...
- CF992B Nastya Studies Informatics 数学(因子) 暴力求解 第三道
Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input st ...
- 算法训练 Hankson的趣味题
算法训练 Hankson的趣味题 时间限制:1.0s 内存限制:64.0MB 问题描述 Hanks 博士是BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫Han ...
- 1172 Hankson 的趣味题[数论]
1172 Hankson 的趣味题 2009年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Descrip ...
- 1172 Hankson 的趣味题
1172 Hankson 的趣味题 2009年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Descrip ...
- Codevs 1172 Hankson 的趣味题 2009年NOIP全国联赛提高组
1172 Hankson 的趣味题 2009年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description Hanks 博 ...
- 一本通1626【例 2】Hankson 的趣味题
1626:[例 2]Hankson 的趣味题 题目描述 Hanks 博士是BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫Hankson.现在,刚刚放学回家的Hankson 正在思考 ...
- 洛谷 P1072 Hankson 的趣味题 解题报告
P1072 \(Hankson\)的趣味题 题目大意:已知有\(n\)组\(a0,a1,b0,b1\),求满足\((x,a0)=a1\),\([x,b0]=b1\)的\(x\)的个数. 数据范围:\( ...
随机推荐
- opencv3.0机器学习算法使用
//随机树分类Ptr<StatModel> lpmlBtnClassify::buildRtreesClassifier(Mat data, Mat responses, int ntra ...
- 方便快捷组织页面 DOM 的 js 引模板擎 —— doT.js 的使用
—————————————————————————————————————————— ——————————————————————————————————————————
- Win7 node多版本管理gnvm采坑记录
采坑描述:下载新node版本及切换node失败 解决:1.要用管理员权限启动cmd:2.确保node是空闲的 Gnvm下载地址: 32-bit | 64-bit Github 1.下载之后为 得到一个 ...
- Bootstrap-模态框 modal.js
参考网址:http://v3.bootcss.com/(能抄不写) 1.大模态框 图片效果图: 代码:(button的属性data-target对应的是具体模态框的class) <!-- Lar ...
- Java算法练习——两数相加
题目链接 题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新 ...
- VBA单元格自适应高亮操作
1.单元格所在行和列高亮 第一种方式 Private Sub worksheet_selectionchange(ByVal target As Range) Cells.Interior.Color ...
- HDU_2256 矩阵快速幂 需推算
最近开始由线段树转移新的内容,线段树学到扫描线这里有点迷迷糊糊的,有时候放一放可能会好一些. 最近突然对各种数学问题很感兴趣.好好钻研了一下矩阵快速幂.发现矩阵真是个计算神器,累乘类的运算原本要O(N ...
- 代码杂谈-python函数
发现函数可以设置属性变量, 如下 newfunc.func , newfunc.args def partial(func, *args, **keywords): """ ...
- Linux无法连接网络解决方案
上次在VM中装好Linux以后,用xshell可以连接上Linux,可是今天在启动虚拟机打开Linux以后,发现又没有网络连接了,因为要用xshell连接的话首先要知道Linux的ipv4地址,在li ...
- SQL基础教程(第2版)第8章 SQL高级处理:8-1 窗口函数
第8章 SQL高级处理:8-1 窗口函数 ● 窗口函数可以进行排序.生成序列号等一般的聚合函数无法实现的高级操作.● 理解PARTITION BY和ORDER BY这两个关键字的含义十分重要. ■什么 ...