HDU6128 二次剩余/二次域求二次剩余解/LL快速乘法取模
题意:求满足模p下$\frac{1}{a_i+a_j}\equiv\frac{1}{a_i}+\frac{1}{a_j}$的对数,其中$n,p(1\leq n\leq10^5,2\leq p\leq10^{18})$
思路:推式子,两边同乘$(a_i + a_j)^3$,得$a_i^2+a_j^2 \equiv {a_i·a_j} \mod{p}$,进一步$a_i^2+a_j^2+a_i·a_j\equiv {0} \mod{p}$,然后?然后会点初中数竞,或者数感好会因式分解就能看出来,两边再乘个$a_i-a_j$就是$a_i^3-a_j^3$了,直接得到了$a_i$和$a_j$的关系,可喜可贺。然而显然我这种蠢得的人看不出来的,一般性的我们使用一元二次求根公式,视$a_j$为常数,得在模p下的解$a_i=a_j·\frac{-1 \pm \sqrt{-3}}{2}$,那么得到了$a_i$和$a_j$的关系,但是有$\sqrt{-3}$的存在,也就是要想办法求到$x^2 \equiv{-3} \mod{p}$的解,来替代原式中的根号,这个头次见到还是浙大校赛的ZOJ3774,当时还奇怪为什么有人能直接看出xx数就是$\sqrt{5}$的二次剩余。
这里用到二次剩余的相关知识,前置知识是看二次剩余有关的教学ppt(欧拉判别,勒让德符号之类的),求二次剩余的解有种二次域的求法(参见wiki上的Cipolla's algorithm),还有这位菊苣的博客,acdream菊苣的博客上也有模板和一些介绍......
因为半桶水,证明或算法在此不表。
其他要注意给出数据有可能为0,且要进行欧拉判别模p下是否存在二次剩余为-3的解,以及p=3的情况,还有就是因为LL下乘法溢出的问题,注意使用O(1)的$2^{64}$LL的取模乘法。
- /** @Date : 2017-08-17 20:07:47
- * @FileName: 1009.cpp
- * @Platform: Windows
- * @Author : Lweleth (SoungEarlf@gmail.com)
- * @Link : https://github.com/
- * @Version : $Id$
- */
- #include <bits/stdc++.h>
- #define LL long long
- #define PII pair<int ,int>
- #define MP(x, y) make_pair((x),(y))
- #define fi first
- #define se second
- #define PB(x) push_back((x))
- #define MMG(x) memset((x), -1,sizeof(x))
- #define MMF(x) memset((x),0,sizeof(x))
- #define MMI(x) memset((x), INF, sizeof(x))
- using namespace std;
- const int INF = 0x3f3f3f3f;
- const int N = 1e5+20;
- const double eps = 1e-8;
- LL mod;
- struct T
- {
- LL p, d;
- };
- LL w;
- //O1乘法取模黑科技
- LL mul(LL x,LL y)
- {
- return (x * y-(LL)(x /(long double)mod * y + 1e-3) * mod + mod) % mod;
- }
- //二次域乘法
- T multi_er(T a, T b)
- {
- T ans;
- ans.p = (mul(a.p, b.p) + mul(mul(a.d,b.d), w)) % mod;
- ans.d = (mul(a.p, b.d) + mul(a.d, b.p)) % mod;
- return ans;
- }
- LL quick_mod(LL a, LL b)
- {
- LL ans = 1;
- a %= mod;
- while(b)
- {
- if(b & 1)
- {
- ans = mul(ans , a);
- b--;
- }
- b >>= 1;
- a = mul(a , a);
- }
- return ans;
- }
- //二次域上快速幂
- T power(T a, LL b)
- {
- T ans;
- ans.p = 1;
- ans.d = 0;
- while(b)
- {
- if(b & 1)
- {
- ans = multi_er(ans, a);
- b--;
- }
- b >>= 1;
- a = multi_er(a, a);
- }
- return ans;
- }
- //求勒让德符号
- LL Legendre(LL a, LL p)
- {
- return quick_mod(a, (p-1)>>1);
- }
- LL QuadraticResidue()
- {
- LL rem = (-3 % mod + mod) % mod;
- if(rem == 0)//特判mod==3
- return 0;
- if(mod == 2)//特判非奇素数
- return 1;
- if(Legendre(rem, mod) + 1 == mod)//欧拉判别条件 非剩余
- return -1;
- LL b;
- while(1)//找一个非剩余求二次域上的单位w=sqrt(b^2 - rem)
- {
- b = rand() % mod;
- w = (mul(b, b) - rem + mod) % mod;
- if(quick_mod(w, (mod - 1)/2) + 1 == mod)//cipolla
- break;
- }
- T tmp;
- tmp.p = b;
- tmp.d = 1;
- T ans = power(tmp, (mod + 1) / 2);
- return ans.p;
- }
- vector<LL>a;
- int main()
- {
- int T;
- cin >> T;
- while(T--)
- {
- a.clear();
- LL n, p;
- scanf("%lld%lld", &n, &mod);
- for(int i = 0; i < n; i++)
- {
- LL t;
- scanf("%lld", &t);
- if(t > 0)//注意有0的...
- a.PB(t);
- }
- LL cnt = a.size();
- sort(a.begin(), a.end());
- ///////////
- LL ans = 0;
- if(mod == 2)//特殊情况无解
- ans = cnt * (cnt - 1) / 2LL;
- else
- {
- LL t = QuadraticResidue();
- if(t == -1)
- {
- printf("0\n");
- continue;
- }
- LL inv = (mod + 1) >> 1;
- LL x = mul((mod + t - 1LL)%mod, inv);
- LL y = mul((mod - t - 1LL)%mod, inv);
- for(int i = 0; i < cnt; i++)
- {
- LL tmp = mul(x , a[i]);
- ans += upper_bound(a.begin(), a.begin() + i, tmp)
- - lower_bound(a.begin(), a.begin() + i, tmp);
- }
- if(x != y)//两个解
- {
- for(int i = 0; i < cnt; i++)
- {
- LL tmp = mul(y, a[i]);
- ans += upper_bound(a.begin(), a.begin() + i, tmp)
- - lower_bound(a.begin(), a.begin() + i, tmp);
- }
- }
- }
- printf("%lld\n", ans);
- }
- return 0;
- }
- /*
- 99
- 5 7
- 1 2 3 4 5
- 6 7
- 1 2 3 4 5 6
- */
HDU6128 二次剩余/二次域求二次剩余解/LL快速乘法取模的更多相关文章
- 九度OJ 1085 求root(N, k) -- 二分求幂及快速幂取模
题目地址:http://ac.jobdu.com/problem.php?pid=1085 题目描述: N<k时,root(N,k) = N,否则,root(N,k) = root(N',k). ...
- The 2018 ACM-ICPC China JiangSu Provincial Programming Contest快速幂取模及求逆元
题目来源 The 2018 ACM-ICPC China JiangSu Provincial Programming Contest 35.4% 1000ms 65536K Persona5 Per ...
- 二分求幂/快速幂取模运算——root(N,k)
二分求幂 int getMi(int a,int b) { ; ) { //当二进制位k位为1时,需要累乘a的2^k次方,然后用ans保存 == ) { ans *= a; } a *= a; b / ...
- 【转】C语言快速幂取模算法小结
(转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速 ...
- HDU 1061 Rightmost Digit --- 快速幂取模
HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...
- UVa 11582 (快速幂取模) Colossal Fibonacci Numbers!
题意: 斐波那契数列f(0) = 0, f(1) = 1, f(n+2) = f(n+1) + f(n) (n ≥ 0) 输入a.b.n,求f(ab)%n 分析: 构造一个新数列F(i) = f(i) ...
- HDU--杭电--4506--小明系列故事——师兄帮帮忙--快速幂取模
小明系列故事——师兄帮帮忙 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) To ...
- CodeForces Round #191 (327C) - Magic Five 等比数列求和的快速幂取模
很久以前做过此类问题..就因为太久了..这题想了很久想不出..卡在推出等比的求和公式,有除法运算,无法快速幂取模... 看到了 http://blog.csdn.net/yangshuolll/art ...
- HDU1013,1163 ,2035九余数定理 快速幂取模
1.HDU1013求一个positive integer的digital root,即不停的求数位和,直到数位和为一位数即为数根. 一开始,以为integer嘛,指整型就行吧= =(too young ...
随机推荐
- c# 修改pdf
继续引用spire的dll. 1.代码如下: PdfDocument doc = new PdfDocument(); doc.LoadFromFile("wen.pdf"); P ...
- centos7环境下mysql安装
1.去官网下载合适的yum源安装包 https://dev.mysql.com/downloads/repo/yum/ 2.yum 本地安装 命令:yum localinstall mysql57-c ...
- URL相关Web APIs
参考文档:MDN> Web API接口 > URLUtils MDN > Web API接口 > URL MDN > Web API接口 > Location MD ...
- c文法
程序→<外部声明>|<程序> 外部声明→<功能定义>|<声明> 功能定义→<声明复合语句的类型> 类型→<VOID| CHAR| IN ...
- PAT L1 - 046 整除光棍
https://pintia.cn/problem-sets/994805046380707840/problems/994805084284633088 这里所谓的“光棍”,并不是指单身汪啦~ 说的 ...
- (转)微软借力.NET开源跨平台支持,布局物联网平台开发
今天科技类最大的新闻,莫过于微软宣布.NET开发框架开源计划..NET 开源,集成 Clang 和 LLVM 并且自带 Android 模拟器,这意味着 Visual Studio 这个当下最好没有之 ...
- 【C++】深度探索C++对象模型读书笔记--构造函数语义学(The Semantics of constructors)(四)
成员们的初始化队伍(member Initia 有四种情况必须使用member initialization list: 1. 当初始化一个reference member时: 2. 当初始化一个co ...
- centos中apache自用常用额外配置记录(xwamp)
xwamp套件中apache配置,记录下,以免忘记. 配置路径 ${wwwroot_dir}/conf/httpd.conf 配置内容 <ifmodule mod_deflate.c> D ...
- day 008 文件操作
08. 万恶之源-⽂文件操作本节主要内容:1. 初识⽂文件操作2. 只读(r, rb)3. 只写(w, wb)4. 追加(a, ab)5. r+读写6. w+写读7. a+写读(追加写读)8. 其他操 ...
- C++解析(15):二阶构造模式
0.目录 1.构造函数与半成品对象 2.二阶构造 3.小结 1.构造函数与半成品对象 关于构造函数: 类的构造函数用于对象的初始化 构造函数与类同名并且没有返回值 构造函数在对象定义时自动被调用 问题 ...