POJ2115 C Looooops[扩展欧几里得]
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 24355 | Accepted: 6788 |
Description
for (variable = A; variable != B; variable += C)
statement;
I.e., a loop which starts by setting variable to value A and while variable is not equal to B, repeats statement followed by increasing the variable by C. We want to know how many times does the statement get executed for particular values of A, B and C, assuming that all arithmetics is calculated in a k-bit unsigned integer type (with values 0 <= x < 2k) modulo 2k.
Input
The input is finished by a line containing four zeros.
Output
Sample Input
3 3 2 16
3 7 2 16
7 3 2 16
3 4 2 16
0 0 0 0
Sample Output
0
2
32766
FOREVER
Source
(A+s*C)%2^k=B
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
ll A,B,C,k;
inline void exgcd(ll a,ll b,ll &g,ll &x,ll &y){
if(b==){x=;y=;g=a;}
else{exgcd(b,a%b,g,y,x);y-=x*(a/b);}
}
int main(){
while(scanf("%lld%lld%lld%lld",&A,&B,&C,&k)!=EOF){
if(!A&&!B&&!C&&!k) break;
ll c=B-A,a=C,b=1LL<<k,g,x,y;
exgcd(a,b,g,x,y);
if(c%g) printf("FOREVER\n");
else{
b/=g;c/=g;
printf("%lld\n",(x%b*c%b+b)%b);
}
}
}
POJ2115 C Looooops[扩展欧几里得]的更多相关文章
- poj2115 C Looooops——扩展欧几里得
题目:http://poj.org/problem?id=2115 就是扩展欧几里得呗: 然而忘记除公约数... 代码如下: #include<iostream> #include< ...
- C Looooops(扩展欧几里得+模线性方程)
http://poj.org/problem?id=2115 题意:给出A,B,C和k(k表示变量是在k位机下的无符号整数),判断循环次数,不能终止输出"FOREVER". 即转化 ...
- [POJ2115]C Looooops 拓展欧几里得
原题入口 这个题要找到本身的模型就行了 a+c*x=b(mod 2k) -> c*x+2k*y=b-a 求这个方程对于x,y有没有整数解. 这个只要学过拓展欧几里得(好像有的叫扩展欧几里德QA ...
- C Looooops(扩展欧几里得)
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20128 Accepted: 5405 Descripti ...
- POJ 2115 C Looooops(扩展欧几里得)
辗转相除法(欧几里得算法) 时间复杂度:在O(logmax(a, b))以内 int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a ...
- POJ 2115 C Looooops扩展欧几里得
题意不难理解,看了后就能得出下列式子: (A+C*x-B)mod(2^k)=0 即(C*x)mod(2^k)=(B-A)mod(2^k) 利用模线性方程(线性同余方程)即可求解 模板直达车 #incl ...
- POJ 2115 C Looooops(扩展欧几里得应用)
题目地址:POJ 2115 水题. . 公式非常好推.最直接的公式就是a+n*c==b+m*2^k.然后能够变形为模线性方程的样子,就是 n*c+m*2^k==b-a.即求n*c==(b-a)mod( ...
- POJ - 2115C Looooops 扩展欧几里得(做的少了无法一眼看出)
题目大意&&分析: for (variable = A; variable != B; variable += C) statement;这个循环式子表示a+c*n(n为整数)==b是 ...
- 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 ...
随机推荐
- C#怎样保证弹出窗体是唯一并居中显示
Winform窗体中,假如我从Form1窗体要弹出Form2窗体,写法是这样的: Form2 f2 = new Form2(); f2.Show(); 1.如何使窗体打开时居中显示 //初始化默认窗体 ...
- svn中cleanup作用
我们开发项目的时候,用版本控制软件svn提交项目时候难免会遇到cleanup,那么这个提示是怎么产生的呢?它有什么作用呢? 产生原因:SVN 本地更新时,由于一些操作中断更新,如磁盘空间不够,用户取消 ...
- No.008:String to Integer (atoi)
问题: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- Linux(二)__文件目录、常用命令
一.linux下的文件目录 linux的文件系统是采用级层式的树状目录结构,在此结构中的最上层 是根目录"/",然后在此目录下再创建其他的目录 深刻理解linux文件目录是非常重要 ...
- 新建 ASP.NET Core MVC 项目 -- Hello World!
一.创建一个空项目 请查看 新建 .NET Core 项目 -- Hello World! 一节,新建一个项目: 二.添加引用并修改配置为 MVC 修改 .vscode\launch.json 文件 ...
- c#模拟js escape方法
public static string Escape(string s) { StringBuilder sb = new StringBuilder(); byte[] ba = System.T ...
- Thinkcmf 二次开发
一. 创建模板 demo 1 Tpl下创建demo文件-----后台启用新的模板 (网站信息--模板方案) 2 在模板在tpl/demo目录下创建Portal目录,然后在Portal目录下创建in ...
- 七牛--关于图片上传方向不统一的问题--主要关于图片EXIF信息中旋转参数Orientation的理解
[图片引用方向纠正]直接在图片后面添加 ?imageMogr/auto-orient eg:http://data.upfitapp.com/data/2016/10/18/1629114767606 ...
- css之水平居中设置
行内元素: div,p{text-align:center;} 定宽块状元素: 第一宽度固定,第二margin-left和margin-right均是auto.div{border:1px ...
- js 20160810
jquery 获取不到所有相同id 的元素 ,只能获取此id的第一个元素.可以获取所有相同class 的元素