题目: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. 【Nginx】事件驱动框架和异步处理

    Nginx对请求的处理是通过事件触发的,模块作为事件消费者,仅仅能被事件收集.分发器调用.这与传统的Webserver是不同的. 传统的Webserver下,一个请求由一个进程消费.请求在建立连接后将 ...

  2. CSDN - 进程结束后new出的内存会回收吗?

    http://blog.csdn.net/stanjiang2010/article/details/5386647     关键词:内存回收  

  3. 系统安全-SElinux

    Selinux是[security-Enhanced Linux]的简称,是美国国家安全局[NSA]和[SCC]开发的Linux的一个扩张强制访问控制安全模块. 因为企业的业务平台的服务器上存储着大量 ...

  4. Linux面试必问-对照目录内容的命令“Diff”具体解释

    dir1下有个log_1.log dir2下有个log_2.log 两个文件例如以下: p_ylwu@VM_194_111_sles10_64:/home/jdxochen/exercise> ...

  5. nanonets

    https://app.nanonets.com/ List of Models IMAGES   Images: Image Categorization Beta Input: Image Out ...

  6. js实现网页端复制功能

    实现网页端复制功能: <div id="copyInput" style="display:none;"> <form> <inp ...

  7. 将txt文件数据存入excel表格

    前言 最近使用Appium自动化在测试设备配网的情况,需要记录每次成功与否和耗时时间. 由于App不是很稳定,执行一段时间会奔溃,因此数据只能通过追加的形式写入到txt文件. 实现过程 存储在txt文 ...

  8. EasyRTMP实现Demux解析MP4文件进行rtmp推送实现RTMP直播功能

    本文转自EasyDarwin团队Kim的博客:http://blog.csdn.net/jinlong0603/article/details/52965101 前面已经介绍过EasyRTMP,这里不 ...

  9. JQuery 如何获取select选中的值

    一.html代码 <select id="ddl"> <option value="100" emoney="12" &g ...

  10. 解决Pods Unable to find a specification for `xxxxx`问题

    错误信息为 Unable to find a specification for *RMQClient* (~> 1.x.x) depended upon by Podfile 刚开始以为这个已 ...