题目大意:有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。这里就变成了同模方程,扩展欧几里得即可解答。

暴力代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. const int maxn=+;
  7. const int mod=;
  8. int in[maxn];
  9.  
  10. int main()
  11. {
  12. //freopen("in.txt","r",stdin);
  13. int t;
  14. scanf("%d",&t);
  15. for(int i=; i<t; i++)
  16. scanf("%d",in+i);
  17. bool flag;
  18. for(int a=; a<=; a++)
  19. {
  20. for(int b=; b<=; b++)
  21. {
  22. flag=false;
  23. for(int i=; i<t; i++)
  24. if(in[i]!=((a*(a*in[i-]%mod+b)+b)%mod))
  25. {
  26. flag=true;
  27. break;
  28. }
  29. if(!flag)
  30. {
  31. for(int i=; i<t; i++)
  32. printf("%d\n",(a*in[i]+b)%mod);
  33. break;
  34. }
  35. }
  36. if(!flag)
  37. break;
  38. }
  39. return ;
  40. }

扩展欧几里得:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. const int maxn=+;
  7. const int mod=;
  8. int in[maxn];
  9. typedef long long ll;
  10.  
  11. ll exgcd(ll a, ll b, ll&x, ll&y)
  12. {
  13. if (b == )
  14. {
  15. x = ;
  16. y = ;
  17. return a;
  18. }
  19. ll r = exgcd(b, a%b, y, x);
  20. ll t = x;
  21. y = y - a/b*t;
  22. return r;
  23. }
  24. int main()
  25. {
  26. //freopen("in.txt","r",stdin);
  27. int t;
  28. scanf("%d",&t);
  29. for(int i=; i<t; i++)
  30. scanf("%d",in+i);
  31. bool flag;
  32. for(ll a=; a<=; a++)
  33. {
  34. ll x,y; //定义long long 型是保证没有取模的式子不会超内存
  35. ll g=exgcd(a+,mod,x,y);
  36. ll tmp=in[]-a*a*in[]; //这里可以先不取模,后面计算b的时候取模
  37. if(tmp%g==)
  38. {
  39. flag=false;
  40. ll b=(x*tmp/g)%mod; //这里最好取下模,虽然后面计算in[i]的时候也会取模,但是算出来的in[i]可能因为b负太多而变成负数
  41. for(int i=;i<t;i++)
  42. {
  43. if(in[i]!=(a*(a*in[i-]+b)+b)%mod)
  44. {
  45. flag=true;
  46. break;
  47. }
  48. }
  49. if(!flag)
  50. {
  51. for(int i=;i<t;i++)
  52. printf("%d\n",(a*in[i]+b)%mod);
  53. break;
  54. }
  55. }
  56.  
  57. }
  58. return ;
  59. }

UVA 12169 Disgruntled Judge 枚举+扩展欧几里得的更多相关文章

  1. UVA 12169 Disgruntled Judge【扩展欧几里德】

    题意:随机选取x1,a,b,根据公式xi=(a*xi-1+b)%10001得到一个长度为2*n的序列,奇数项作为输入,求偶数项,若有多种,随机输出一组答案. 思路:a和b均未知,可以考虑枚举a和b,时 ...

  2. UVA 12169 Disgruntled Judge 扩展欧几里得

    /** 题目:UVA 12169 Disgruntled Judge 链接:https://vjudge.net/problem/UVA-12169 题意:原题 思路: a,b范围都在10000以内. ...

  3. UVA.12169 Disgruntled Judge ( 拓展欧几里得 )

    UVA.12169 Disgruntled Judge ( 拓展欧几里得 ) 题意分析 给出T个数字,x1,x3--x2T-1.并且我们知道这x1,x2,x3,x4--x2T之间满足xi = (a * ...

  4. UVa 12169 Disgruntled Judge 紫书

    思路还是按照紫书,枚举a,得出b, 然后验证. 代码参考了LRJ的. #include <cstdio> #include <iostream> using namespace ...

  5. 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 ...

  6. UVa 12169 (枚举+扩展欧几里得) Disgruntled Judge

    题意: 给出四个数T, a, b, x1,按公式生成序列 xi = (a*xi-1 + b) % 10001 (2 ≤ i ≤ 2T) 给出T和奇数项xi,输出偶数项xi 分析: 最简单的办法就是直接 ...

  7. UVa 12169 - Disgruntled Judge(拓展欧几里德)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. UVA 12169 Disgruntled Judge(Extended_Euclid)

    用扩展欧几里德Extended_Euclid解线性模方程,思路在注释里面了. 注意数据范围不要爆int了. /********************************************* ...

  9. UVA 12169 Disgruntled Judge

    我该怎么说这道题呢...说简单其实也简单,就枚举模拟,开始卡了好久,今天看到这题没a又写了遍,看似会超时的代码交上去a了,果然实践是检验真理的唯一标准... #include <iostream ...

随机推荐

  1. FIR.im Weekly - 劳动节我们也没有停下来

    五一到五四的节假日对勤劳的开发者们似乎是零存在,各种干货好资源也并未因假期的到来而减少,所以这周的 Weekly 依然多产. Swift 样式指南:2015年4月更新 这是 @开发技术前线 收录的由 ...

  2. git忽略以点开头的文件夹

    git忽略以点开头的文件夹 好像不是什么问题,可是我用的时候不好使,还是记录下 参考:http://www.oschina.net/question/1437985_2181276

  3. KnockoutJS 3.X API 第二章 数据监控(1)视图模型与监控

    数据监控 KO的三个内置核心功能: 监控(Observable)和依赖性跟踪(dependency tracking) 声明绑定(Declarative bindings) 模板(Templating ...

  4. winform 程序制作自己的数字签名(续)

    在上一篇文章<winform 程序制作自己的数字签名>中我们已经可以得到我们程序定制的数字签名了,但是比较讨厌的是每次编译之后,数字签名需要重新手动添加. 我们需要的是在程序编译时自动添加 ...

  5. Android listview addHeaderView 和 addFooterView 详解

    addHeaderView()方法:主要是向listView的头部添加布局addFooterView()方法:主要是向listView的底部添加布局 需要注意的是添加布局的时候应该添加从父容器开始添加 ...

  6. NUMA架构的CPU -- 你真的用好了么?

    本文从NUMA的介绍引出常见的NUMA使用中的陷阱,继而讨论对于NUMA系统的优化方法和一些值得关注的方向. 文章欢迎转载,但转载时请保留本段文字,并置于文章的顶部 作者:卢钧轶(cenalulu) ...

  7. Java多线程系列--“基础篇”09之 interrupt()和线程终止方式

    概要 本章,会对线程的interrupt()中断和终止方式进行介绍.涉及到的内容包括:1. interrupt()说明2. 终止线程的方式2.1 终止处于“阻塞状态”的线程2.2 终止处于“运行状态” ...

  8. ZooKeeper官方文档翻译——ZooKeeper Overview 3.4.6

    ZooKeeper ZooKeeper: A Distributed Coordination Service for Distributed Applications (针对分布式应用的分布式调度服 ...

  9. java并发编程(4)--线程池的使用

    转载:http://www.cnblogs.com/dolphin0520/p/3932921.html 一. java中的ThreadPoolExecutor类 java.util.concurre ...

  10. caffe pytho接口

    一:搭建Caffe 1.下载happynear的Caffe源码https://www.github.com/happynear/caffe-windows,第三方库3rdparty文件http://p ...