hdu 1576 求逆元
题意:给出n=A mod 9973和B,求(A/B) mod 9973
昨天用扩展欧几里得做过这题,其实用逆元也可以做。
逆元的定义:例如a*b≡1 (mod m),则b就是a关于m的逆元。
求逆元方法也很简单,用扩展欧几里得解这个方程即可。
逆元性质:若a是b的逆元,则(x/a)mod p=(x*b)mod p
对于本题呢?设B的逆元为x,
那么有(A/B) mod 9973=((A mod 9973)*(x mod 9973))mod 9973
Reference: http://blog.csdn.net/leonharetd/article/details/13095191
#include <iostream>
using namespace std;
__int64 a,b,b1,x,k,tm,r,T,n,ans; __int64 extend_gcd(__int64 a,__int64 b,__int64 &x,__int64 &y)
{
if (b==)
{
x=;
y=;
return a;
}
else
{
__int64 r=extend_gcd(b,a%b,y,x);
y=y-x*(a/b);
return r;
}
} int gcd(int a,int b)
{
if (b==) return a;
return gcd(b,a%b);
} int main()
{
cin>>T;
while (T--)
{
cin>>n>>b;
//bx==1 (mod m)
tm=extend_gcd(b,,x,k);
b1=x*(/tm);
r=/tm;
b1=(b1%r+r)%r; //求出最小非负整数解
ans=(n*(b1%))%;
cout<<ans<<endl; }
return ;
}
hdu 1576 求逆元的更多相关文章
- HDU 1576 (乘法逆元)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1576 题目大意:求(A/B)mod 9973.但是给出的A是mod形式n,n=A%9973. 解题思 ...
- 【hdu 1576】A/B(数论--拓展欧几里德 求逆元 模版题)
题意:给出 A%9973 和 B,求(A/B)%9973的值. 解法:拓展欧几里德求逆元.由于同余的性质只有在 * 和 + 的情况下一直成立,我们要把 /B 转化为 *B-1,也就是求逆元. 对于 B ...
- HDU 5768Lucky7(多校第四场)容斥+中国剩余定理(扩展欧几里德求逆元的)+快速乘法
地址:http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Time Limit: 2000/1000 MS (Java/Others) M ...
- hdu 1576 A/B 【扩展欧几里得】【逆元】
<题目链接> <转载于 >>> > A/B Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)( ...
- HDU 5407 CRB and Candies(LCM +最大素因子求逆元)
[题目链接]pid=5407">click here~~ [题目大意]求LCM(Cn0,Cn1,Cn2....Cnn)%MOD 的值 [思路]来图更直观: 这个究竟是怎样推出的.说实话 ...
- hdu_1576A/B(扩展欧几里得求逆元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 A/B Time Limit: 1000/1000 MS (Java/Others) Me ...
- HDU 1576 A/B 数论水题
http://acm.hdu.edu.cn/showproblem.php?pid=1576 写了个ex_gcd的模板...太蠢导致推了很久的公式 这里推导一下: 因为 1 = BX + 9973Y ...
- HDU 1576 A/B (两种解法)
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 分析:等式枚举法,由题意可得:, ,代入 , 得:,把变量 合在一起得: :即满足 为 倍 ...
- CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)
Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...
随机推荐
- java9-3 返回类型
1. 返回值类型 基本类型:(基本类型简单) 引用类型: 类:返回的是该类的对象 class Student2 { public void study() { System.out.println(& ...
- [原创]gerrit上分支操作记录(创建分支、删除分支)
Git分支对于一个项目的代码管理而言,是十分重要的! 许多久用git的朋友可能已经掌握的很牢固了,但对于一些初涉git的童鞋来说,可能还不是很熟悉. 在此,我将自己的一些操作经历做一梳理,希望能帮助到 ...
- HYSBZ 2145 悄悄话
2145: 悄悄话 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 271 Solved: 104[Submit][Status][Discuss] ...
- 010医疗项目-模块一:用户添加的实现(Dao,Service,Action,增加页面调试,提交页面调试)
要实现的效果:
- 补鞋匠---Cobbler 服务器自动搭建
Cobbler 服务器自动搭建http://tshare365.com/archives/439.html
- python 转 exe -- py2exe库实录
本文基于windows 7 + python 3.4 把python程序打包成exe,比较好用的库是py2exe 其操作步骤是: --> 编写python程序 --> 再额外编写一个导入了 ...
- tkinter 改变按钮状态
import tkinter as tk def btn1_change_btn1(event): '''方式一:通过事件控制自己''' if event.widget['state'] == 'no ...
- [CareerCup] 8.8 Othello Game 黑白棋游戏
8.8 Othello is played as follows: Each Othello piece is white on one side and black on the other. Wh ...
- [CareerCup] 13.7 Node Pointer 节点指针
13.7 Write a method that takes a pointer to a Node structure as a parameter and returns a complete c ...
- Hadoop的Server及其线程模型分析
早期的一篇文章,针对Hadoop 2.6.0. 一.Listener Listener线程,当Server处于运行状态时,其负责监听来自客户端的连接,并使用Select模式处理Accept事件. 同时 ...