UVA 12169 Disgruntled Judge 枚举+扩展欧几里得
题目大意:有3个整数 x[1], a, b 满足递推式x[i]=(a*x[i-1]+b)mod 10001。由这个递推式计算出了长度为2T的数列,现在要求输入x[1],x[3],......x[2T-1], 输出x[2],x[4]......x[2T]. T<=100,0<=x<=10000. 如果有多种可能的输出,任意输出一个结果即可。
由于a和b都小于等于10000,直接枚举a和b暴力可以过。但是有没有更快的方法呢?
首先令递推式的i=2,那么x[2]=(a*x[1]+b)mod 10001;再令i=3,得x[3]=(a*x[2]+b)mod 10001,可以得出x[3]=(a*(a*x[1]+b)+b)mod 10001。这时候只有a和b是变量,我们枚举a,就可以求出b了。(a+1)*b mod 10001 = ( (x[3]-a*a*x[1]) mod 10001 + 10001 ) mod 10001.(这里的x[3]-a*a*x[1]可能为负,代码中可以先不取模,后面计算b的时候一起取模即可) 所以简化成(a+1)*b mod 10001 = (x[3]-a*a*x[1]) mod 10001。这里就变成了同模方程,扩展欧几里得即可解答。
暴力代码:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- using namespace std;
- const int maxn=+;
- const int mod=;
- int in[maxn];
- int main()
- {
- //freopen("in.txt","r",stdin);
- int t;
- scanf("%d",&t);
- for(int i=; i<t; i++)
- scanf("%d",in+i);
- bool flag;
- for(int a=; a<=; a++)
- {
- for(int b=; b<=; b++)
- {
- flag=false;
- for(int i=; i<t; i++)
- if(in[i]!=((a*(a*in[i-]%mod+b)+b)%mod))
- {
- flag=true;
- break;
- }
- if(!flag)
- {
- for(int i=; i<t; i++)
- printf("%d\n",(a*in[i]+b)%mod);
- break;
- }
- }
- if(!flag)
- break;
- }
- return ;
- }
扩展欧几里得:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- using namespace std;
- const int maxn=+;
- const int mod=;
- int in[maxn];
- typedef long long ll;
- ll exgcd(ll a, ll b, ll&x, ll&y)
- {
- if (b == )
- {
- x = ;
- y = ;
- return a;
- }
- ll r = exgcd(b, a%b, y, x);
- ll t = x;
- y = y - a/b*t;
- return r;
- }
- int main()
- {
- //freopen("in.txt","r",stdin);
- int t;
- scanf("%d",&t);
- for(int i=; i<t; i++)
- scanf("%d",in+i);
- bool flag;
- for(ll a=; a<=; a++)
- {
- ll x,y; //定义long long 型是保证没有取模的式子不会超内存
- ll g=exgcd(a+,mod,x,y);
- ll tmp=in[]-a*a*in[]; //这里可以先不取模,后面计算b的时候取模
- if(tmp%g==)
- {
- flag=false;
- ll b=(x*tmp/g)%mod; //这里最好取下模,虽然后面计算in[i]的时候也会取模,但是算出来的in[i]可能因为b负太多而变成负数
- for(int i=;i<t;i++)
- {
- if(in[i]!=(a*(a*in[i-]+b)+b)%mod)
- {
- flag=true;
- break;
- }
- }
- if(!flag)
- {
- for(int i=;i<t;i++)
- printf("%d\n",(a*in[i]+b)%mod);
- break;
- }
- }
- }
- return ;
- }
UVA 12169 Disgruntled Judge 枚举+扩展欧几里得的更多相关文章
- UVA 12169 Disgruntled Judge【扩展欧几里德】
题意:随机选取x1,a,b,根据公式xi=(a*xi-1+b)%10001得到一个长度为2*n的序列,奇数项作为输入,求偶数项,若有多种,随机输出一组答案. 思路:a和b均未知,可以考虑枚举a和b,时 ...
- UVA 12169 Disgruntled Judge 扩展欧几里得
/** 题目:UVA 12169 Disgruntled Judge 链接:https://vjudge.net/problem/UVA-12169 题意:原题 思路: a,b范围都在10000以内. ...
- UVA.12169 Disgruntled Judge ( 拓展欧几里得 )
UVA.12169 Disgruntled Judge ( 拓展欧几里得 ) 题意分析 给出T个数字,x1,x3--x2T-1.并且我们知道这x1,x2,x3,x4--x2T之间满足xi = (a * ...
- UVa 12169 Disgruntled Judge 紫书
思路还是按照紫书,枚举a,得出b, 然后验证. 代码参考了LRJ的. #include <cstdio> #include <iostream> using namespace ...
- Codeforces Round #451 (Div. 2) B. Proper Nutrition【枚举/扩展欧几里得/给你n问有没有两个非负整数x,y满足x·a + y·b = n】
B. Proper Nutrition time limit per test 1 second memory limit per test 256 megabytes input standard ...
- UVa 12169 (枚举+扩展欧几里得) Disgruntled Judge
题意: 给出四个数T, a, b, x1,按公式生成序列 xi = (a*xi-1 + b) % 10001 (2 ≤ i ≤ 2T) 给出T和奇数项xi,输出偶数项xi 分析: 最简单的办法就是直接 ...
- UVa 12169 - Disgruntled Judge(拓展欧几里德)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 12169 Disgruntled Judge(Extended_Euclid)
用扩展欧几里德Extended_Euclid解线性模方程,思路在注释里面了. 注意数据范围不要爆int了. /********************************************* ...
- UVA 12169 Disgruntled Judge
我该怎么说这道题呢...说简单其实也简单,就枚举模拟,开始卡了好久,今天看到这题没a又写了遍,看似会超时的代码交上去a了,果然实践是检验真理的唯一标准... #include <iostream ...
随机推荐
- FIR.im Weekly - 劳动节我们也没有停下来
五一到五四的节假日对勤劳的开发者们似乎是零存在,各种干货好资源也并未因假期的到来而减少,所以这周的 Weekly 依然多产. Swift 样式指南:2015年4月更新 这是 @开发技术前线 收录的由 ...
- git忽略以点开头的文件夹
git忽略以点开头的文件夹 好像不是什么问题,可是我用的时候不好使,还是记录下 参考:http://www.oschina.net/question/1437985_2181276
- KnockoutJS 3.X API 第二章 数据监控(1)视图模型与监控
数据监控 KO的三个内置核心功能: 监控(Observable)和依赖性跟踪(dependency tracking) 声明绑定(Declarative bindings) 模板(Templating ...
- winform 程序制作自己的数字签名(续)
在上一篇文章<winform 程序制作自己的数字签名>中我们已经可以得到我们程序定制的数字签名了,但是比较讨厌的是每次编译之后,数字签名需要重新手动添加. 我们需要的是在程序编译时自动添加 ...
- Android listview addHeaderView 和 addFooterView 详解
addHeaderView()方法:主要是向listView的头部添加布局addFooterView()方法:主要是向listView的底部添加布局 需要注意的是添加布局的时候应该添加从父容器开始添加 ...
- NUMA架构的CPU -- 你真的用好了么?
本文从NUMA的介绍引出常见的NUMA使用中的陷阱,继而讨论对于NUMA系统的优化方法和一些值得关注的方向. 文章欢迎转载,但转载时请保留本段文字,并置于文章的顶部 作者:卢钧轶(cenalulu) ...
- Java多线程系列--“基础篇”09之 interrupt()和线程终止方式
概要 本章,会对线程的interrupt()中断和终止方式进行介绍.涉及到的内容包括:1. interrupt()说明2. 终止线程的方式2.1 终止处于“阻塞状态”的线程2.2 终止处于“运行状态” ...
- ZooKeeper官方文档翻译——ZooKeeper Overview 3.4.6
ZooKeeper ZooKeeper: A Distributed Coordination Service for Distributed Applications (针对分布式应用的分布式调度服 ...
- java并发编程(4)--线程池的使用
转载:http://www.cnblogs.com/dolphin0520/p/3932921.html 一. java中的ThreadPoolExecutor类 java.util.concurre ...
- caffe pytho接口
一:搭建Caffe 1.下载happynear的Caffe源码https://www.github.com/happynear/caffe-windows,第三方库3rdparty文件http://p ...