当前位置: 首页 > news >正文

UVA - 12169 Disgruntled Judge(枚举+扩展欧几里得)

题目链接


这道题一开始并不知道怎么做,尽管想到了枚举,但是枚举的时间复杂度为 O ( t ∗ n 2 ) O(t*n^2) O(tn2),然后就没多想了(但是UVA牛掰的评测机似乎可以跑过)

看LRJ的分析是,想办法得到 a a a,然后再计算出 b b b。不难想到可以枚举 a a a,但是表达式中的取模似乎很难解决,然后就卡住了…

实际上是我太年轻,这点东西都没想到:已知 x 3 = [ a ( ( a x 1 + b ) % 10001 ) + b ] % 10001 x_3=[a((ax_1+b)\%10001)+b]\%10001 x3=[a((ax1+b)%10001)+b]%10001,化简过程中所有的模 10001 10001 10001替换成一个未知项 10001 ∗ ( − k ) 10001*(-k) 10001(k),即:

x 3 = a 2 x 1 + ( a + 1 ) ∗ b + 10001 ∗ ( − k ) x_3=a^2x_1+(a+1)*b+10001*(-k) x3=a2x1+(a+1)b+10001(k) x 3 , x 1 , a x_3,x_1,a x3,x1,a已知,那么移项可得:

( a + 1 ) ∗ b + 10001 ∗ ( − k ) = x 3 − a 2 x 1 (a+1)*b+10001*(-k)=x_3-a^2x_1 (a+1)b+10001(k)=x3a2x1

当然此方程式有解的充要条件为: ( x 3 − a 2 x 1 ) % g c d ( a + 1 , 10001 ) = = 0 (x_3-a^2x_1)\%gcd(a+1,10001)==0 (x3a2x1)%gcd(a+1,10001)==0,那么我们用扩欧解方程式 ( a + 1 ) ∗ b + 10001 ∗ ( − k ) = g c d ( a + 1 , 10001 ) (a+1)*b+10001*(-k)=gcd(a+1,10001) (a+1)b+10001(k)=gcd(a+1,10001)即可

时间复杂度 O ( n ∗ t ∗ l o g n ) O(n*t*logn) O(ntlogn)

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ins insert
#define lowbit(x) (x&(-x))
#define mkp(x,y) make_pair(x,y)
#define mem(a,x) memset(a,x,sizeof a);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> P;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const ll INF=1e18;
const int Mod=1e9+7;
const int maxn=10001;

int t;
ll x[210];

ll gcd(ll a,ll b){
    return b==0?a:gcd(b,a%b);
}

ll exgcd(ll a,ll b,ll &x,ll &y){
    if(!b){
        x=1,y=0;
        return a;
    }
    ll gcd=exgcd(b,a%b,y,x);
    y-=(a/b)*x;
    return gcd;
}

bool check(ll a,ll b){
    for(int i=1;i<=2*t;i++){
        if(i&1){
            if(i>1){
                ll res=(a*x[i-1]+b)%maxn;
                if(res!=x[i]) return false;
            }
        }else{
            x[i]=(a*x[i-1]+b)%maxn;
        }
    }
    return true;
}

void print(){
    for(int i=2;i<=2*t;i+=2)
        printf("%lld\n",x[i]);
}

int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int a,b;
    scanf("%d",&t);
    for(int i=1;i<=2*t;i+=2)
        scanf("%d",&x[i]);
    for(int i=0;i<maxn;i++){
        ll c=x[3]-i*i*x[1],a=i+1,b=maxn;
        ll g=gcd(a,b);
        if(c%g==0){
            ll x,y;
            exgcd(a,b,x,y);
            ll p=c/g;
            x*=p;
            if(check(i,x)){
                print();
                return 0;
            }
        }
    }
    return 0;
}

相关文章:

  • ASM管理命令和操作笔记
  • UVA - 10791 Minimum Sum LCM(质因数分解)
  • 删除ASM残留信息方法和重建步骤
  • UVA - 12716 GCD XOR(找规律+枚举技巧)
  • Oracle 修改归档模式
  • UVA - 1635 Irrelevant Elements(质因数分解)
  • spfile和pifle的一点浅浅的认识
  • 欧拉函数
  • UVA - 1636 Headshot(条件概率)
  • Oracle RAC日常基本维护命令
  • UVA - 11181 Probability|Given(条件概率+状压dfs)
  • UVA - 1637 Double Patience(全概率+记忆化搜索)
  • Oracle检查对象[第八章笔记]
  • 魔法数字(dfs/bfs)
  • Win32 OpenGL编程(8) 3D模型变换及其组合应用
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • Angular 响应式表单 基础例子
  • css系列之关于字体的事
  • git 常用命令
  • JavaSE小实践1:Java爬取斗图网站的所有表情包
  • Java多态
  • JS+CSS实现数字滚动
  • Mithril.js 入门介绍
  • nodejs实现webservice问题总结
  • oldjun 检测网站的经验
  • React-Native - 收藏集 - 掘金
  • React-生命周期杂记
  • SQLServer插入数据
  • SwizzleMethod 黑魔法
  • VUE es6技巧写法(持续更新中~~~)
  • 爱情 北京女病人
  • 从零开始在ubuntu上搭建node开发环境
  • 动手做个聊天室,前端工程师百无聊赖的人生
  • 二维平面内的碰撞检测【一】
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 简单基于spring的redis配置(单机和集群模式)
  • 区块链分支循环
  • CMake 入门1/5:基于阿里云 ECS搭建体验环境
  • elasticsearch-head插件安装
  • ​LeetCode解法汇总1410. HTML 实体解析器
  • ​水经微图Web1.5.0版即将上线
  • #多叉树深度遍历_结合深度学习的视频编码方法--帧内预测
  • (八)c52学习之旅-中断实验
  • (附源码)springboot车辆管理系统 毕业设计 031034
  • *(长期更新)软考网络工程师学习笔记——Section 22 无线局域网
  • **PyTorch月学习计划 - 第一周;第6-7天: 自动梯度(Autograd)**
  • .net core IResultFilter 的 OnResultExecuted和OnResultExecuting的区别
  • .net 获取url的方法
  • .Net 知识杂记
  • .Net(C#)常用转换byte转uint32、byte转float等
  • @FeignClient注解,fallback和fallbackFactory
  • @JsonFormat与@DateTimeFormat注解的使用
  • @NoArgsConstructor和@AllArgsConstructor,@Builder
  • @SuppressLint(NewApi)和@TargetApi()的区别
  • @在php中起什么作用?