【XR-4】题
题面
题解
由题,所求为方程\(y^2 = x^2 + ax + b\)的整数解数量。
两边同乘\(4\),可得\((2y)^2 = 4x^2 + 4ax + 4b\)。
配方后得\((2y)^2 = (2x + a)^2 + 4b - a^2\)。
移项得\((2y + 2x + a)(2y - 2x - a) = 4b - a^2\)。
于是将\(4b - a^2\)的约数求出来,解一个二元一次方程就行了。
同时如果\(4b - a^2 = 0\),那么此时如果\(a\)是偶数输出inf
,\(a\)是奇数输出0
。
又\(|4b - a^2|\)最大可能达到\(10^{16}\),分解质因数可能需要Pollard_Rho
。
代码
#include <cstdio>
#include <algorithm>
#include <vector>
long long A, B, C; int ans, f;
long long Mul(long long x, long long y, long long Mod)
{ return (__int128) x * y % Mod; }
long long fastpow(long long x, long long y, long long Mod)
{
long long ans = 1;
for (; y; y >>= 1, x = Mul(x, x, Mod))
if (y & 1) ans = Mul(ans, x, Mod);
return ans;
}
bool Miller_Rabin(long long x)
{
if (x == 2) return true;
if ((x & 1) == 0) return false;
for (int T = 10; T; T--)
{
long long a = 1ll * rand() * rand() % (x - 2) + 2;
if (fastpow(a, x - 1, x) != 1) return false;
long long p = x - 1;
while (!(p & 1))
{
p >>= 1; long long t = fastpow(a, p, x);
if (Mul(t, t, x) == 1 && t != 1 && t != x - 1) return false;
}
}
return true;
}
long long Pollard_Rho(long long n)
{
if ((n & 1) == 0) return 2;
long long c = 1ll * rand() * rand() % (n - 1) + 1;
long long i = 0, k = 2, x = 1ll * rand() * rand() % (n - 1) + 1, y = x;
while (1)
{
++i, x = (Mul(x, x, n) + c) % n;
long long d = std::__gcd((y - x + n) % n, n);
if (d != 1 && d != n) return d;
if (x == y) return n;
if (i == k) y = x, k <<= 1;
}
}
std::vector<long long> fac;
void Fact(long long n)
{
if (n == 1) return;
if (Miller_Rabin(n)) return (void) (fac.push_back(n));
long long p = n; while (p == n) p = Pollard_Rho(n);
Fact(p), Fact(n / p);
}
__int128 sqr(__int128 x) { return x * x; }
int check(long long p, long long q)
{
long long xy = p - A, yx = q + A; int cnt = 0;
if ((abs(xy) & 1) || (abs(yx) & 1) || ((p + q) & 3)) return 0;
long long x = (xy + yx) / 4, y = (xy - x * 2) / 2;
if (x >= 0 && y >= 0) ++cnt;
return cnt;
}
void dfs(long long x, int dep)
{
ans += check(x, f * C / x);
for (int j = dep; j < (int) fac.size(); j++)
{
long long t = fac[j], s = x * t;
for (; C % s == 0; s = s * t) dfs(s, j + 1);
}
}
int main()
{
scanf("%lld%lld", &A, &B); C = B * 4 - A * A, f = 1;
if (C < 0) C = -C, f = -1; if (C == 0) return printf(A & 1 ? "0" : "inf"), 0;
Fact(C), std::sort(fac.begin(), fac.end());
fac.erase(std::unique(fac.begin(), fac.end()), fac.end());
dfs(1, 0), printf("%d\n", ans);
return 0;
}
【XR-4】题的更多相关文章
- C算法编程题(五)“E”的变换
前言 上一篇<C算法编程题(四)上三角> 插几句话,说说最近自己的状态,人家都说程序员经常失眠什么的,但是这几个月来,我从没有失眠过,当然是过了分手那段时期.每天的工作很忙,一个任务接一个 ...
- tyvj P1716 - 上帝造题的七分钟 二维树状数组区间查询及修改 二维线段树
P1716 - 上帝造题的七分钟 From Riatre Normal (OI)总时限:50s 内存限制:128MB 代码长度限制:64KB 背景 Background 裸体就意味着 ...
- 基于Visual C++2013拆解世界五百强面试题--题7-链表的各种操作
请用C实现一个链表,实现链表的查找,逆置,替换,删除,添加,清空,创建. 查找.替换和删除.添加里面都会用到遍历链表的操作,所以重点在于遍历, 链表的逆置和清空考虑到效率,我们可以用递归实现, 至于创 ...
- 【20171026早】alert(1) to win - 第六、七、八题
早上7点起床,又写了一篇小说发在了起点网上,有兴趣的可以看看.点击这里 忙完后,继续练习,刚开始发现自己答题的速度有些慢,可能是因为对于html,javascript知识不是很精通,但是话又说回来,谁 ...
- ●线段树的三个题(poj 3225,hdu 1542,hdu 1828)
●poj 3225 Help with Intervals(线段树区间问题) ○赘述题目 给出以下集合操作: 然后有初始的一个空集S,和以下题目给出的操作指令,并输入指令: 要求进行指令操作后,按格式 ...
- AtCoder Grand Contest 11~17 做题小记
原文链接https://www.cnblogs.com/zhouzhendong/p/AtCoder-Grand-Contest-from-11-to-20.html UPD(2018-11-16): ...
- AtCoder Grand Contest 1~10 做题小记
原文链接https://www.cnblogs.com/zhouzhendong/p/AtCoder-Grand-Contest-from-1-to-10.html 考虑到博客内容较多,编辑不方便的情 ...
- 数列分块入门九题(二):LOJ6280~6282
Preface 个人感觉这中间的三题是最水的没有之一 数列分块入门 4--区间加法,区间求和 这个也是很多数据结构完爆的题目线段树入门题,但是练分块我们就要写吗 修改还是与之前类似,只不过我们要维护每 ...
- 2019寒假练题计划——LibreOJ刷题计划 &《信息学奥赛一本通》提高版题目
目录 2019.1.27 #10082. 「一本通 3.3 例 1」Word Rings 题意 思路 #10083. 「一本通 3.3 例 2」双调路径 题意 思路 #10084. 「一本通 3.3 ...
- 【noip模拟题】天神下凡(贪心)
vijos某次模拟赛原题... 处理出每个圆的一级祖先就行了... 其实没有那么麻烦,贪心即可出解. 我们将每个圆转换成线段后按左端点小右端点大的方法排序 然后维护一个栈: 对于每一个圆i 如果栈顶右 ...
随机推荐
- Java 8 New Features
What's New in JDK 8 https://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html Java Pla ...
- IntelliJ Cannot find declaration to goto----解决方案
系统中已经有了该类库,还是找不到类提示 close the project in intellij. close intellij. go to the project folder and dele ...
- H3C 什么是漫游
- PAT 乙级 1076.Wifi密码
题目来源 下面是微博上流传的一张照片:“各位亲爱的同学们,鉴于大家有时需要使用 wifi,又怕耽误亲们的学习,现将 wifi 密码设置为下列数学题答案:A-1:B-2:C-3:D-4:请同学们自己作答 ...
- springboot-注解-@Repository、@Service、@Controller 和 @Component
Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring ...
- 【转】Linux虚拟网络基础——tap
原文:https://blog.csdn.net/chengqiuming/article/details/80071073 ------------------------------------- ...
- 通信、端点、IO、文件
什么是网络套接字(Socket)?一时还真不好回答,而且网络上也有各种解释,莫衷一是.下文将以本人所查阅到的资料来说明一下什么是Socket. Socket定义 Socket在维基百科的定义: A n ...
- redux沉思录
要素:store.reducer.dispatch/subscribe connect:将业务逻辑剥离到容器类,数据的双向绑定: 数据.操作.UI分离.命令封装 核心思想:对共享状态的维护: 核心代码 ...
- IMP self _cmd
The only way to circumvent dynamic binding is to get the address of a method and call it directly as ...
- redux的知名ppt
https://slides.com/jenyaterpil/redux-from-twitter-hype-to-production#/20