UVa 1426 Discrete Square Roots (扩展欧几里德)
题意:给定 x,n,r,满足 r2 ≡ x mod(n) ,求在 0 ~ n 内满足 rr2 ≡ x mod(n) 的所有的 rr。
析:很明显直接是肯定不行了,复杂度太高了。
r2 ≡ x mod(n) (1)
rr2 ≡ x mod(n) (2)
用 (2)- (1)得到
rr2 - r2 ≡ 0 mod (n)
(rr + r)*(rr - r) ≡ 0 mod (n),
可以得到
(rr + r)*(rr - r) = k * n。
假设 n = a * b,
那么 可以知道 (rr + r) % a == 0 && (rr - r) % b == 0 || (rr + r) % b == 0 && (rr - r) % a == 0,
也就是
rr + r = k1 * a (3)
rr - r = k2 * b (4)
(3)-(4)得
k1 * a + k2 * b = 2 * r,
这是一个方程,r 是已知的,然后 a 和 b,可以通过枚举 n 的因子得到,这样就可以解这方程,解出 k1 代入(3),就能得到 rr。
就解决了这个问题,还有这个可能会产生重复的解,所以可以用 set 。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define be begin()
#define ed end()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define aLL 1,n,1
#define FOR(i,n,x) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int maxn = 300 + 20;
const int maxm = 76543;
const int mod = 1e9 + 9;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
inline int readInt(){ int x; scanf("%d", &x); return x; } void exgcd(int a, int b, LL &d, LL &x, LL &y){
if(!b){ d = a; x = 1; y = 0; }
else{ exgcd(b, a%b, d, y, x); y -= (a/b) * x; }
} int main(){
int x, r, kase = 0;
while(scanf("%d %d %d", &x, &n, &r) == 3 && x + n + r){
vector<P> fact;
int t = sqrt(n + 1.);
for(int i = 1; i <= t; ++i) if(n % i == 0) fact.pb(P(i, n/i)), fact.pb(P(n/i, i));
set<int> sets;
sets.insert(r);
LL k1, k2, d;
for(int i = 0; i < fact.sz; ++i){
int a = fact[i].fi;
int b = fact[i].se;
exgcd(a, b, d, k1, k2);
if(2 * r % d) continue;
int bb = abs(b / d);
LL K1 = k1 * 2LL * r / d;
k1 = (K1 % bb + bb) % bb;
k2 = k1;
while(1){
LL rr = k1 * a - r;
if(rr >= 0){
if(rr >= n) break;
sets.insert(rr);
}
k1 += bb;
}
while(1){
LL rr = k2 * a - r;
if(rr <= n){
if(rr < 0) break;
sets.insert(rr);
}
k2 -= bb;
}
}
printf("Case %d:", ++kase);
for(auto &it: sets) printf(" %d", it);
printf("\n");
}
return 0;
}
UVa 1426 Discrete Square Roots (扩展欧几里德)的更多相关文章
- UVA 1426 - Discrete Square Roots(数论)
UVA 1426 - Discrete Square Roots 题目链接 题意:给定X, N. R.要求r2≡x (mod n) (1 <= r < n)的全部解.R为一个已知解 思路: ...
- UVALive - 4270 Discrete Square Roots (扩展欧几里得)
给出一组正整数$x,n,r$,使得$r^2\equiv x(mod\: n)$,求出所有满足该等式的$r$. 假设有另一个解$r'$满足条件,则有$r^2-r'^2=kn$ 因式分解,得$(r+r') ...
- UVA 12169 Disgruntled Judge【扩展欧几里德】
题意:随机选取x1,a,b,根据公式xi=(a*xi-1+b)%10001得到一个长度为2*n的序列,奇数项作为输入,求偶数项,若有多种,随机输出一组答案. 思路:a和b均未知,可以考虑枚举a和b,时 ...
- UVALive 4270 Discrete Square Roots
题目描述: 在已知一个离散平方根的情况下,按照从小到大的顺序输出其他所有的离散平方根. 在模n意义下,非负整数x的离散平方根是满足0<=r<n且r2=x(mod n)的整数r. 解题思路: ...
- Discrete Square Roots UVALive - 4270(拓展欧几里得)
a≡b(mod n)的含义是“a和b除以n的余数相同”,其充要条件是“a-b是n的整数倍”: 求所有满足条件r^2=x(mod m)的r 题目已经给定了一个初始的r,x,m #include < ...
- UVA1426 Discrete Square Roots
思路:\(exgcd\) 提交:\(2\)次 错因:输出格式错误OTZ 题解: 求:\(r^2 ≡ x \mod N , 0 \leq r < N\),并且题目会给出 \(x,N\) 和一个合法 ...
- uva 1426 离散平方根
1426 - Discrete Square Roots Time limit: 3.000 seconds A square root of a number x <tex2html_verb ...
- SGU 106 The equation 扩展欧几里德
106. The equation time limit per test: 0.25 sec. memory limit per test: 4096 KB There is an equation ...
- (扩展欧几里德算法)zzuoj 10402: C.机器人
10402: C.机器人 Description Dr. Kong 设计的机器人卡尔非常活泼,既能原地蹦,又能跳远.由于受软硬件设计所限,机器人卡尔只能定点跳远.若机器人站在(X,Y)位置,它可以原地 ...
随机推荐
- python学习笔记之四-多进程&多线程&异步非阻塞
ProcessPoolExecutor对multiprocessing进行了高级抽象,暴露出简单的统一接口. 异步非阻塞 爬虫 对于异步IO请求的本质则是[非阻塞Socket]+[IO多路复用]: & ...
- 2-zookeeper、ha
1.zookeeper ①背景: Zookeeper 最早起源于雅虎研究院的一个研究小组.在当时,研究人员发现,在雅虎内部很多大型系统基本都需要依赖一个类似的系统来进行分布式协调, 但是这些系统往往都 ...
- MySQL 存储修改
真的坑.
- C#与Excel的交互示例
//这里加添加一个Excel对象的包装器.就是添加一个引用 using System; using System.Drawing; using System.Collections; using Sy ...
- CSS vertical-align属性详解
. 首页 博客园 联系我 前言:关于vertical-align属性. 实践出真知. 垂直居中. 第二种用法. 留言评论 返回顶部 前言:关于vertical-align属性 vertical-ali ...
- # 20175213 2018-2019-2 《Java程序设计》第1周学习总结
在本周的java学习中,我收获了很多也遇到了很多的困难1.在寒假的预学习中,因为没能完全的安好虚拟机,导致在本周的学习一开始,虚拟机就崩溃了,所以又重新开始重头安装虚拟机.但因为网速等各种问题,虚拟机 ...
- 作业注释 CSS表单1 输入框前有图片
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- layui-tree创建下拉树型选项框
1.简介 也许你对树菜单再熟悉不过了,然而你仍有可能更青睐layui的tree,它也许比以往你知道的任何一个类似的组件都更轻量.简单,你在享受许多功能的同时,甚至不用去记太多的参数. 另外,最大的重点 ...
- python note 06 编码方式
1.有如下值li= [11,22,33,44,55,66,77,88,99,90],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中.即: {'k1': 大 ...
- 247. Strobogrammatic Number II输出所有对称数字
[抄题]: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at u ...