题目: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. class文件无论是32位还是64位jdk编译出来的,都可以通用

    class文件无论是32位还是64位jdk编译出来的,都可以通用 学习了:https://blog.csdn.net/z3111001358/article/details/53364066 java ...

  2. javascript之Ajax获取和设置标头

    XMLHttpRequest对象中与标头有关的方法: setRequestHeader(<header>,<value>)--用指定值设置标头. getResponseHead ...

  3. at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:)

    错误提示 错误原因 參考链接 错误提示: at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:) 在Andro ...

  4. Fckeditor常见漏洞的挖掘与利用整理汇总

    查看编辑器版本号 FCKeditor/_whatsnew.html ------------------------------------------------------------- 2. V ...

  5. selector模块使用

    #服务端 from socket import * import selectors sel=selectors.DefaultSelector() def accept(server_fileobj ...

  6. 使用Auto Layout处理比例间距问题

    使用Auto Layout处理比例间距问题 Auto Layout 是一个掌握起来很具有挑战性的东西.iOS 9引入的 Stack Views和 layout 锚点有一些帮助,但是明白如何创建特定的 ...

  7. oracle sql修改序列为当前序列开始

    declare   v_num integer;  last_value integer;Begin  select SEQ_TBM_ID.NEXTVAL into last_value from d ...

  8. VS + Qt5Designer + Anaconda环境配置

    最近打算做一个模型训练工具,从来都不喜欢做UI的我,最终把目光放在了QtDesigner上.配环境的过程中在网上翻阅了不少博客,但大多是pycharm或者是VScode,使用VS的似乎不多.所以打算记 ...

  9. bootstrap-table自己配置

    function initTable(){ var methodNameSearch=$("#methodNameSearch").val(); var requestUrl =  ...

  10. nginx的源代码分析--间接回调机制的使用和类比

    nginx使用了间接回调机制.结合upstream机制的使用来说明一下,首先明白几个事实: 1)事实上ngxin和下游client的连接使用的是ngx_http_connection_t,每一个连接相 ...