Problem Description

要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973)= 1)。

Input

数据的第一行是一个T,表示有T组数据。

每组数据有两个数n(0 <= n < 9973)和B(1<= B <= 10^9)。

Output

对应每组数据输出(A/B)%9973。

Sample Input

2

1000 53

87 123456789

Sample Output

7922

6060

/*************************

欧几里得算法,又称辗转相除法,用于求最大公约数:  c = gcd(a,b) = gcd(b,a%b);

扩展欧几里得算法:

若a,b不全为0,则存在x,y使得 c = gcd(a,b) = a*x+b*y;

因为 gcd(a,b) = gcd(b,a%b),则有 x*a + y * b = x1 * b + y1 * (a%b),等式右边变形,则得到:b * x1 + (a%b) * y1 = b * x1+(a-a/b*b) * y1 = a * y1+b * (x1 - ( a / b) * y1)。

则  x = y1 , y= x1-(a/b)*y1,然后可由后向前迭代得到 x,y。

1573 A/B:

A%B = 0 可令 x = A / B -> A = B*x;

n = A % 9973 = A - A / 9973 * 9973 :

= B*x - A/9973*9973

= n

可以 令 y = A / 9973;  则 B * x + y * 9973 = n;

因为 gcd(9973,B) = 1,所以不能用exgcd(B,9973,x,y);

B * x1 + 9973 * y1 = 1; -> B * x1 *n + 9973 * y1 * n = n;

红色字体的两个式子比较系数就可以看出 x = x1 * n;

然后求 x % 9973 就OK 了

代码:

#include <iostream>
using namespace std;
int exgcd(int a,int b,int &x,int &y)// 扩展欧几里得函数模板
{
if(b==0)
{
x = 1;y = 0;return a;
}
int d = exgcd(b,a%b,x,y);
int t = x;
x = y;
y = t-a/b*y;
return d;
} int main()
{
int t,n,B,x,y,ans,xx;
cin>>t;
while(t--)
{
cin>>n>>B;
ans = exgcd(B,9973,x,y);
xx = x*n;
ans = (xx%9973+9973)%9973; // 防止 x 为负数
cout<<ans<<endl;
}
}

hdu 1573 A/B (扩展欧几里得)的更多相关文章

  1. 扩展欧几里得 hdu 1576

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 不知道扩展欧几里得的同学可以参考:https://blog.csdn.net/zhjchengf ...

  2. hdu 5512 Pagodas 扩展欧几里得推导+GCD

    题目链接 题意:开始有a,b两点,之后可以按照a-b,a+b的方法生成[1,n]中没有的点,Yuwgna 为先手, Iaka后手.最后不能再生成点的一方输: (1 <= n <= 2000 ...

  3. hdu 1576 A/B 【扩展欧几里得】【逆元】

    <题目链接> <转载于 >>> > A/B Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)( ...

  4. HDU 5114 扩展欧几里得

    题目大意:给你两个球的坐标 他们都往(1, 1)这个方向以相同的速度走,问你他们在哪个位置碰撞. 思路:这种题目需要把x方向和y方向分开来算周期,两个不同周期需要用扩展欧几里得来求第一次相遇. #in ...

  5. [ACM] hdu 3923 Invoker (Poyla计数,高速幂运算,扩展欧几里得或费马小定理)

    Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael ...

  6. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)

    http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...

  7. poj 2891 扩展欧几里得迭代解同余方程组

    Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...

  8. hdu_1576A/B(扩展欧几里得求逆元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 A/B Time Limit: 1000/1000 MS (Java/Others)    Me ...

  9. Root(hdu5777+扩展欧几里得+原根)2015 Multi-University Training Contest 7

    Root Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Su ...

随机推荐

  1. MVC Model 数据注解与验证

    常用验证特性: using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Sch ...

  2. Python的高级特性之切片、迭代、列表生成式、生成器

    切片 切片就是获取一个list.tuple.字符串等的部分元素 l = range(100) #取[0,5)元素 print(l[:5]) #[0, 1, 2, 3, 4] #在[0,99]中每隔10 ...

  3. [LeetCode] 76. Minimum Window Substring 解题思路

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  4. usaco 打扫食槽

    Description 从前奶牛是不挑食的,但现在世道变了,她们变得非常挑剔.牧场里有N头奶牛,约翰 要向她们提供M种食物,第i头奶牛只会吃Pi号食物. 约翰每天都要打扫食槽,这件事非常累.奶牛沿着食 ...

  5. 命令 "sudo -H" 中的这个 "H" 什么作用?

    脚本中使用$HOME变量 问题描述:某些同事原来写的脚本中包含如下内容. BIN_DIR=${HOME}/tools TAIR_BIN_DIR=${HOME}/tair_bin TAIR_SRC_DI ...

  6. 浙大PTA - - 堆中的路径

    题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21731 本题即考察最小堆的基本操作: #include "iostrea ...

  7. Pgsql数据库jsonb操作函数集合

    CREATE OR REPLACE FUNCTION "json_object_del_path"( "json" json, "key_path&q ...

  8. Quartz定时任务学习(二)web应用

    web中使用Quartz 1.首先在web.xml文件中加入 如下内容(根据自己情况设定) 在web.xml中添加QuartzInitializerServlet,Quartz为能够在web应用中使用 ...

  9. shell查找文件并删除

    -mtime 0 表示文件改动时间距离当前为0天的文件.即距离当前时间不到1天(24小时)以内的文件. -mtime 1 表示文件改动时间距离当前为1天的文件,即距离当前时间1天(24小时-48小时) ...

  10. [Redux] Using mapDispatchToProps() Shorthand Notation

    We will learn how to avoid the boilerplate code in mapDispatchToProps() for the common case where ac ...