POJ2115 C Looooops 模线性方程(扩展欧几里得)
题意:很明显,我就不说了
分析:令n=2^k,因为A,B,C<n,所以取模以后不会变化,所以就是求(A+x*C)%n=B
转化一下就是求 C*x=B-A(%n),最小的x
令a=C,b=B-A
原式等于ax=b(mod n) 这就是标准的解模线性方程
该方程有解的充要条件是d=gcd(n,a) && d|b(可以根据这一条判断是否FOREVER)
然后参考算法导论应用扩展欧几里得求解x
a*x0+n*y0=d
x=x0*(b/d)(mod n)
然后应用多解条件求最小正整数解,即解的最小间距t=n/d,x+=i*t,i=0,1,..d-1
x=(x%t+t)%t
代码:
#include <cstdio>
#include <iostream>
#include <ctime>
#include <vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
LL x,y;
LL Egcd(LL a,LL b)
{
if(b==)
{
x=;
y=;
return a;
}
int res=Egcd(b,a%b);
LL tmp=x;
x=y;
y=tmp-a/b*y;
return res;
}
int main()
{
LL a,b,c,k;
while(~scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k))
{
if(!a&&!b&&!c&&!k)break;
LL n=(1ll<<k);
LL d=Egcd(c,n);
if((b-a)%d)
{
printf("FOREVER\n");
continue;
}
x*=((b-a)/d);
x=(x%(n/d)+n/d)%(n/d);
printf("%I64d\n",x);
}
return ;
}
POJ2115 C Looooops 模线性方程(扩展欧几里得)的更多相关文章
- POJ2115 C Looooops ——模线性方程(扩展gcd)
题目链接:http://poj.org/problem?id=2115 C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- C Looooops(扩展欧几里得+模线性方程)
http://poj.org/problem?id=2115 题意:给出A,B,C和k(k表示变量是在k位机下的无符号整数),判断循环次数,不能终止输出"FOREVER". 即转化 ...
- POJ2115 - C Looooops(扩展欧几里得)
题目大意 求同余方程Cx≡B-A(2^k)的最小正整数解 题解 可以转化为Cx-(2^k)y=B-A,然后用扩展欧几里得解出即可... 代码: #include <iostream> us ...
- 【扩展欧几里得】poj2115 C Looooops
题意大概是让你求(A+Cx) mod 2^k = B的最小非负整数解. 若(B-A) mod gcd(C,2^k) = 0,就有解,否则无解. 式子可以化成Cx + 2^k*y = B - A,可以用 ...
- poj2115 C Looooops——扩展欧几里得
题目:http://poj.org/problem?id=2115 就是扩展欧几里得呗: 然而忘记除公约数... 代码如下: #include<iostream> #include< ...
- POJ1061:青蛙的约会+POJ2115C Looooops+UVA10673Play with Floor and Ceil(扩展欧几里得)
http://poj.org/problem?id=1061 第一遍的写法: #include <iostream> #include <stdio.h> #include & ...
- POJ2115(扩展欧几里得)
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23700 Accepted: 6550 Descr ...
- URAL 1141. RSA Attack(欧拉定理+扩展欧几里得+快速幂模)
题目链接 题意 : 给你n,e,c,并且知道me ≡ c (mod n),而且n = p*q,pq都为素数. 思路 : 这道题的确与题目名字很相符,是个RSA算法,目前地球上最重要的加密算法.RSA算 ...
- 浅谈扩展欧几里得[exgcd] By cellur925
关于扩展欧几里得从寒假时就很迷,抄题解过了同余方程,但是原理并不理解. 今天终于把坑填上了qwq. 由于本人太菜,不会用markdown,所以这篇总结是手写的(什么).(字丑不要嫌弃嘛) ****** ...
随机推荐
- C# Windows - Button 控件
.Net Framework提供了一个派生于Control的类System.Windows.Forms.ButtonBase,它实现了Button控件所需的基本功能. System.Windows.F ...
- tableView的基本使用(改良版)
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate> { int i;//用来计算接受通知的次数 ...
- Xcode 向6.0以后版本添加iOS开发空白模板
打开finder,找到应用程序,找到xcode 右键显示包内容.按照如下目录进行查找:Contents ▸ Developer ▸ Platforms ▸ iPhoneOS.platform ▸ De ...
- Machine Learning for Developers
Machine Learning for Developers Most developers these days have heard of machine learning, but when ...
- spoj 346
当一个数大于等于12 那分别处以2, 3, 4之后的和一定大于本身 但是直接递归会超时 然后发现有人用map存了 膜拜..... #include <cstdio> #i ...
- 设置UINavigation的背景图片和背景颜色
//通过背景图片来设置背景 float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue]; UIImage * ...
- Kafka server的的停止
这算是CountDownLatch的一个典型使用场景. kafka.Kafka对象的main方法中与此有关的代码为 // attach shutdown handler to catch contro ...
- Win32 DLL和MFC DLL 中封装对话框
现在最常看见的关于DLL的问题就是如何在DLL中使用对话框,这是一个很普遍的关于如何在DLL中使用资源的问题.这里我们从Win32 DLL和MFC DLL两个方面来分析并解决这个问题. ...
- Ubuntu环境下手动配置openSSH
配置openSSH 1.手动下载压缩文件(.tar.gz) zlib-1.2.7.tar.gz openssl-1.0.1j.tar.gz openssh-6.0p1.tar.gz 2.安装zlib ...
- BZOJ 3123 SDOI2013 森林
首先对于查询操作就是裸的COT QAQ 在树上DFS建出主席树就可以了 对于连接操作,我们发现并没有删除 所以我们可以进行启发式合并,每次将小的树拍扁插入大的树里并重构即可 写完了之后第一个和第二个点 ...