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内置函数整理
1. abs() 函数返回数字的绝对值 2 divmod() 函数把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b). 3, input() 相等于 eval(ra ...
- PHP判断是手机端还是PC访问
function isMobile(){ $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ' ...
- CentOS内网机器利用iptables共享公网IP上网
公司有个业务是2B的以及日活不大,所以两台服务器搞定,一个6M EIP.两台机器都是CentOS7系统EIP为 xxx.xxx.xxx.xxx绑在 内网ip为 172.18.30.175的服务器上,内 ...
- playbook
1 --- - hosts: web-server 3 remote_user: root tasks: - name: stop logstash shell: PID=` $PID &&a ...
- ReactiveX 学习笔记(22)使用 RxJS + Angular 进行 GUI 编程
课题 程序界面由3个文本编辑框和1个文本标签组成. 要求文本标签实时显示3个文本编辑框所输入的数字之和. 文本编辑框输入的不是合法数字时,将其值视为0. 3个文本编辑框的初值分别为1,2,3. 创建工 ...
- ViewParent 和 ViewManager
ViewGroup 继承了 View 实现了两个接口 ViewParent 和 ViewManager 接口 ViewParent:定义了成为一个View的parent的一些“职能”,当paren ...
- 解决bootstrap和easyUI部分css类冲突问题。
今天发现bootstrap和easyui的css类重复用了一个很笨的办法解决了,这种小事网上都不好搜啊. 我先引用的bootstrap后引用的easy UI,bootstrap的会被覆盖,boot的样 ...
- CORSFilter 跨域资源访问
CORS 定义 Cross-Origin Resource Sharing(CORS)跨来源资源共享是一份浏览器技术的规范,提供了 Web 服务从不同域传来沙盒脚本的方法,以避开浏览器的同源策略,是 ...
- APIcloud微信支付和支付宝支付(方案2,主要在后台进行)
支付宝代码 var aliPay = api.require('aliPay'); api.ajax({ url: yuming+'index.php/api/Alipay/getOrder', me ...
- 你确定你真的懂Nginx与PHP的交互?
Nginx是俄国人最早开发的Webserver,现在已经风靡全球,相信大家并不陌生.PHP也通过二十多年的发展来到了7系列版本,更加关注性能.这对搭档在最近这些年,叱咤风云,基本上LNMP成了当下的标 ...