POJ2115:C Looooops——题解
http://poj.org/problem?id=2115
题目大意:for(i=A;i!=B;i+=C),i的类型的范围为0<=a<1<<k
exgcd裸题目。
设a=C,b=(1<<k),c=(B-A).
则ax+by=c.
#include<cstdio>
#include<cctype>
#include<iostream>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
void exgcd(ll a,ll b,ll &x,ll &y){
if(b==){
x=;y=;
return;
}
exgcd(b,a%b,x,y);
ll temp;
temp=x;
x=y;
y=temp-(a/b)*y;
return;
}
int main(){
ll A,B,C,k;
while(cin>>A>>B>>C>>k){
if(A==B&&B==C&&C==k&&A==)return ;
ll x,y;
ll a=C,b=(ll)<<k,c=B-A;
if(!c){
printf("0\n");
continue;
}
ll g=gcd(a,b);
if(c%g){
printf("FOREVER\n");
continue;
}
a/=g;b/=g;c/=g;
exgcd(a,b,x,y);
x=(x%b*c%b+b)%b;
printf("%lld\n",x);
}
return ;
}
#include<cstdio>
#include<cctype>
#include<iostream>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
void exgcd(ll a,ll b,ll &x,ll &y){
if(b==0){
x=1;y=0;
return;
}
exgcd(b,a%b,x,y);
ll temp;
temp=x;
x=y;
y=temp-(a/b)*y;
return;
}
int main(){
ll A,B,C,k;
while(cin>>A>>B>>C>>k){
if(A==B&&B==C&&C==k&&A==0)return 0;
ll x,y;
ll a=C,b=(ll)1<<k,c=B-A;
if(!c){
printf("0\n");
continue;
}
ll g=gcd(a,b);
if(c%g){
printf("FOREVER\n");
continue;
}
a/=g;b/=g;c/=g;
exgcd(a,b,x,y);
x=(x%b*c%b+b)%b;
printf("%lld\n",x);
}
return 0;
}
POJ2115:C Looooops——题解的更多相关文章
- poj2115 C Looooops(exgcd)
poj2115 C Looooops 题意: 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束. 若在有限次内结束,则输出循环次数. 否则输出死循环. ...
- POJ2115——C Looooops(扩展欧几里德+求解模线性方程)
C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...
- POJ2115 C Looooops 扩展欧几里德
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2115 题意 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次 ...
- POJ2115 C Looooops ——模线性方程(扩展gcd)
题目链接:http://poj.org/problem?id=2115 C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ2115 C Looooops[扩展欧几里得]
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24355 Accepted: 6788 Descr ...
- POJ2115 - C Looooops(扩展欧几里得)
题目大意 求同余方程Cx≡B-A(2^k)的最小正整数解 题解 可以转化为Cx-(2^k)y=B-A,然后用扩展欧几里得解出即可... 代码: #include <iostream> us ...
- poj2115 C Looooops
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29262 Accepted: 8441 Descr ...
- POJ2115 C Looooops(线性同余方程)
无符号k位数溢出就相当于mod 2k,然后设循环x次A等于B,就可以列出方程: $$ Cx+A \equiv B \pmod {2^k} $$ $$ Cx \equiv B-A \pmod {2^k} ...
- 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 ...
随机推荐
- 虚拟机安装win7 64位-完美解决-费元星
安装虚拟机是为了安装一个oracle ,在本机安装 ,本机会卡死,不是每次启动电脑都用oralce,而且有时候服务是关不干净的,所以安装在虚拟机里,需要的时候在开启,特做此记录! 费元星版权Q[971 ...
- nginx下pagespeed使用详解
目录 1.简介 2.安装 2.1脚本安装 查看该脚本的如何使用 使用脚本自动安装 替换以前的nginx 2.2 手动安装 先安装基本依赖 构建pagespeed 重新编译安装nginx 3.配置 3. ...
- 【paging_Class 分页类】使用说明
类名:paging_Class 说明:分页类 注意: 1) 支持百万级数据分页 2) 支持多种类型的SQL语法,比如 Left Join 等. 3) 自动保存查询中的错误情况,记录保存在:/Cache ...
- python数据分析基础——pandas Tutorial
参考pandas官方文档: http://pandas.pydata.org/pandas-docs/stable/10min.html#min 1.pandas中的数据类型 Series 带有索引标 ...
- JDK源码分析:Short.java
Short是基本数据类型short的包装类. 1)声明部: public final class Short extends Number implements Comparable<Short ...
- 【halcon】算子
算子 rgb1_to_gray 灰度化 threshold:英文是阈的意思 二值化算子 Connection Compute connected components of a region. ...
- POJ 3675 Telescope(简单多边形和圆的面积交)
Description Updog is watching a plane object with a telescope. The field of vision in the telescope ...
- POJ 1113 Wall(计算几何の凸包)
Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall ...
- iostat lsof
转至:http://www.51testing.com/html/48/202848-242043.html 命令总结: 1. top/vmstat 发现 wa%过高,vmstat b >1: ...
- 异常概念和处理机制,try-catch-finally,throw和throws,自定义异常
异常概念和处理机制 什么是异常? 所谓异常就是指在程序运行的过程中发生的一些不正常事件.(如除0溢出,数组下标越界,所要读取的文件不存在); 异常导致的后果? Java程序的执行过程中如出现异常事件, ...