题目:https://www.luogu.org/problemnew/show/P1072

思路是把每个数质因数分解,答案对于每个质因数的次数有选择的区间,通过这个计算。

指数的限制就是上限是b1,下限是a1;a0-a1后有剩余的自己不能有;b1-b0有剩余的自己不能剩(即必须满上限)。

分解质因数用了那个好像是 O( n^(1/4) ) 的方法。其实如果给的都是大质数是不是会被卡?

然后写了无比冗长的代码。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int T,a0,a1,b0,b1,p[N],tot,hi[N],lo[N],hi2[N],lo2[N],lm[N],ans;
bool flag=;
int rdn()
{
int ret=;bool fx=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')fx=;ch=getchar();}
while(ch>=''&&ch<='') ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return fx?ret:-ret;
}
int main()
{
T=rdn();
while(T--)
{
a0=rdn(); a1=rdn(); b0=rdn(); b1=rdn();
int n=b1; tot=;//limit
for(int i=;i*i<=n;i++)
{
if(n%i==)
{
p[++tot]=i;lo[tot]=hi[tot]=lo2[tot]=hi2[tot]=;
while(n%i==)n/=i,hi[tot]++;
hi2[tot]=hi[tot];
}
}
if(n>)
{
p[++tot]=n;lo[tot]=hi[tot]=lo2[tot]=hi2[tot]=;
hi[tot]=hi2[tot]=;
} flag=;
n=a1; int p0=;//bottom
for(int i=;i*i<=n;i++)
if(n%i==)
{
int d=;
while(n%i==)n/=i,d++;
while(p0<tot&&p[p0]<i)p0++;
if(p[p0]!=i){flag=;break;}
lo[p0]=d;
}
if(flag){puts("");continue;}
if(n>)
{
while(p0<tot&&p[p0]<n)p0++;
if(p[p0]==n)
lo[p0]=;
} flag=;
for(int i=;i<=tot;i++)if(hi[i]<lo[i]){flag=;break;}
if(flag){puts("");continue;} n=b0; p0=;
for(int i=;i*i<=n;i++)
{
if(n%i==)
{
int d=;
while(n%i==)n/=i,d++;
while(p0<tot&&p[p0]<i)p0++;
if(p[p0]!=i)continue;
lo2[p0]=d;//can't leave
}
}
if(n>)
{
while(p0<tot&&p[p0]<n)p0++;
if(p[p0]==n) lo2[p0]=;
}
for(int i=;i<=tot;i++)
{
int d=hi[i]-lo2[i];
if(d) lo2[i]=hi[i]; else lo2[i]=;
} n=a0; p0=;
for(int i=;i*i<=n;i++)
{
if(n%i==)
{
int d=;
while(n%i==)n/=i,d++;
while(p0<tot&&p[p0]<i)p0++;
if(p[p0]!=i)continue;
if(d>lo[p0])hi2[p0]=lo[p0];//can't exist
}
}
if(n>)
{
while(p0<tot&&p[p0]<n)p0++;
if(p[p0]==n)
if(>lo[p0])hi2[p0]=lo[p0];
} for(int i=;i<=tot;i++)
hi[i]=min(hi[i],hi2[i]),lo[i]=max(lo[i],lo2[i]);
ans=; flag=;
for(int i=;i<=tot;i++)
{
if(lo[i]>hi[i]){flag=;break;}
ans*=hi[i]-lo[i]+;
}
printf("%d\n",flag?ans:);
}
return ;
}

洛谷 1072 Hankson 的趣味题——质因数界限讨论的更多相关文章

  1. 洛谷 - P1072 Hankson - 的趣味题 - 质因数分解

    https://www.luogu.org/problemnew/show/P1072 一开始看了一看居然还想放弃了的. 把 \(x,a_0,a_1,b_0,b_1\) 质因数分解. 例如 \(x=p ...

  2. 洛谷 P1072 Hankson 的趣味题 —— 质因数分解

    题目:https://www.luogu.org/problemnew/show/P1072 满足条件的数 x 一定是 a1 的倍数,b1 的因数,a0/a1 与 x/a1 互质,b1/b0 与 b1 ...

  3. 洛谷 P1072 Hankson 的趣味题 解题报告

    P1072 \(Hankson\)的趣味题 题目大意:已知有\(n\)组\(a0,a1,b0,b1\),求满足\((x,a0)=a1\),\([x,b0]=b1\)的\(x\)的个数. 数据范围:\( ...

  4. 洛谷P1072 Hankson 的趣味题

    P1072 Hankson 的趣味题 题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现在,刚刚放学回家的 Hankson 正在思考一 ...

  5. Java实现洛谷 P1072 Hankson 的趣味题

    P1072 Hankson 的趣味题 输入输出样例 输入 2 41 1 96 288 95 1 37 1776 输出 6 2 PS: 通过辗转相除法的推导 import java.util.*; cl ...

  6. 【题解】洛谷P1072 Hankson的趣味题 (gcd和lcm的应用)

    洛谷P1072:https://www.luogu.org/problemnew/show/P1072 思路 gcd(x,a0)=a1 lcm(x,b0)=b1→b0*x=b1*gcd(x,b0) ( ...

  7. 洛谷 P1072 Hankson 的趣味题 || 打质数表的分解质因数

    方法就是枚举,根据b0和b1可以大大减小枚举范围,方法类似这个http://blog.csdn.net/hehe_54321/article/details/76021615 将b0和b1都分解质因数 ...

  8. 洛谷P1072 Hankson 的趣味题(题解)

    https://www.luogu.org/problemnew/show/P1072(题目传送) 数学的推理在编程的体现越来越明显了.(本人嘀咕) 首先,我们知道这两个等式: (a0,x)=a1,[ ...

  9. 洛谷P1072 Hankson的趣味题

    这是个NOIP原题... 题意: 给定 a b c d 求 gcd(a, x) = b && lcm(c, x) = d 的x的个数. 可以发现一个朴素算法是从b到d枚举,期望得分50 ...

随机推荐

  1. MySQL命令学习(一)

    今天我们来学习一下MySQL中的经常使用命令(MySQL中的命令keyword是不区分大写和小写的): (1)show databases; 显示MySQL中的全部database (2)create ...

  2. java 文件的写入和读取

    //写入操作 方法1 OutputStream f = new FileOutputStream("C:/j/j.txt"); f.write("aaaaaaa" ...

  3. ios开发动物园管理 继承多态的实现

    // // main.m // 继承 // // #import <Foundation/Foundation.h> #import "Animal.h" #impor ...

  4. C++算法之 一句话推断一个整数是不是2 的整数次方

    思路:一个整数假设是2的整数次方,那么它的二进制表示中有且仅仅有一位是1,而其它全部位都是0.把这个整数与这个整数减去1之后进行与运算.那么这个整数其中唯一的 1会变为0,这个整数也变为0: 代码: ...

  5. 五、WEB框架基础(1)

    框架与架构 Python语言有很多web框架,主要是四个,企业级框架Django,高并发处理框架Tornado,快速开发框架Flask,自定义协议框架Twisted. 全栈网络框架封装了网络通信/线程 ...

  6. Ajax技术实现页面无刷新跳转

    Ajax实现无刷新显示新的页面 <!DOCTYPE html> <html> <head> <script src="/jquery/jquery- ...

  7. [Python爬虫] Selenium自己主动訪问Firefox和Chrome并实现搜索截图

    前两篇文章介绍了安装.此篇文章算是一个简单的进阶应用吧.它是在Windows下通过Selenium+Python实现自己主动訪问Firefox和Chrome并实现搜索截图的功能. [Python爬虫] ...

  8. 【BZOJ2625】[Neerc2009]Inspection 最小流

    [BZOJ2625][Neerc2009]Inspection Description You are in charge of a team that inspects a new ski reso ...

  9. 发送get和post请求时常用的content-type

    常见的媒体格式类型如下: text/html : HTML格式 text/plain :纯文本格式 text/xml :  XML格式 image/gif :gif图片格式 image/jpeg :j ...

  10. ABAP读取工单状态 STATUS_READ

    *&---------------------------------------------------------------------* *& Report YDEMO_013 ...