题目: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. 如何给redis设置密码

    如何给redis设置密码 学习了:https://blog.csdn.net/qq_35357001/article/details/56835919

  2. SWF代码分析与破解之路 (YueTai VIP视频信息获取工具) Socket续篇

    引言 上一篇 <Socket与站点保密应用 (隐藏链接的视频下载)>大大咧咧地从 WEB 讲 Socket.再到 TCP/IP 等协议,又再讲到 Wireshark 怎样抓IP包分析.最还 ...

  3. DVBS/S2在数字电视系统中的应用 三 (LNB介绍)

    DVBS/S2在数字电视系统中的应用 三 (LNB介绍) 老谢在前面两篇文章中(例如以下).都有提到LNB这一概念. DVBS/S2在数字电视系统中的应用 一 (DVBS/S2接收系统Block Di ...

  4. JavaScript的string方法(demo)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. YUV格式

    http://blog.csdn.net/u011270282/article/details/50696616 http://blog.csdn.net/acs713/article/details ...

  6. node JS 微信开发

    JS-SDK 要点 微信测试号; 扫码登录;无需认证(只是名称统一为微信测试号)http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/logi ...

  7. python super()(转载)

    一.问题的发现与提出 在Python类的方法(method)中,要调用父类的某个方法,在Python 2.2以前,通常的写法如代码段1: 代码段1: class A: def __init__(sel ...

  8. codeforces Looksery Cup 2015 H Degenerate Matrix

    The determinant of a matrix 2 × 2 is defined as follows: A matrix is called degenerate if its determ ...

  9. php 去除html标记-strip_tags和htmlspecialchars的区别

    strip_tags 去掉 HTML 及 PHP 的标记. 语法: string strip_tags(string str); 传回值: 字串 函式种类: 资料处理 内容说明 本函式可去掉字串中包含 ...

  10. Android-基本控件和详解四种布局方式

    转自:https://www.cnblogs.com/ludashi/p/4883915.html 一.常用基本控件 1.TextView 看到Android中的TextView, 我不禁的想到了iO ...