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——题解的更多相关文章

  1. poj2115 C Looooops(exgcd)

    poj2115 C Looooops 题意: 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束. 若在有限次内结束,则输出循环次数. 否则输出死循环. ...

  2. POJ2115——C Looooops(扩展欧几里德+求解模线性方程)

    C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...

  3. POJ2115 C Looooops 扩展欧几里德

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2115 题意 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次 ...

  4. POJ2115 C Looooops ——模线性方程(扩展gcd)

    题目链接:http://poj.org/problem?id=2115 C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  5. POJ2115 C Looooops[扩展欧几里得]

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24355   Accepted: 6788 Descr ...

  6. POJ2115 - C Looooops(扩展欧几里得)

    题目大意 求同余方程Cx≡B-A(2^k)的最小正整数解 题解 可以转化为Cx-(2^k)y=B-A,然后用扩展欧几里得解出即可... 代码: #include <iostream> us ...

  7. poj2115 C Looooops

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29262   Accepted: 8441 Descr ...

  8. POJ2115 C Looooops(线性同余方程)

    无符号k位数溢出就相当于mod 2k,然后设循环x次A等于B,就可以列出方程: $$ Cx+A \equiv B \pmod {2^k} $$ $$ Cx \equiv B-A \pmod {2^k} ...

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

随机推荐

  1. Java: Replace a string from multiple replaced strings to multiple substitutes

    Provide helper methods to replace a string from multiple replaced strings to multiple substitutes im ...

  2. python爬虫实例大全

    WechatSogou [1]- 微信公众号爬虫.基于搜狗微信搜索的微信公众号爬虫接口,可以扩展成基于搜狗搜索的爬虫,返回结果是列表,每一项均是公众号具体信息字典. DouBanSpider [2]- ...

  3. Selenium基础之--01(将浏览器最大化,设置浏览器固定宽、高,操控浏览器前进、后退)

    1,将浏览器最大化 我们知道调用启动的浏览器不是全屏的,这样不会影响脚本的执行,但是有时候会影响我们"观看"脚本的执行. coding=utf-8 from selenium im ...

  4. 【第六章】MySQL日志文件管理

    1.日志文件管理概述: 配置文件:/etc/my.cnf 作用:MySQL日志文件是用来记录MySQL数据库客户端连接情况.SQL语句的执行情况以及错误信息告示. 分类:MySQL日志文件分为4种:错 ...

  5. Mybatis generator自动生成mybatis配置和类信息

    自动生成代码方式两种: 1.命令形式生成代码,详细讲解每一个配置参数. 2.Eclipse利用插件形式生成代码. 安装插件方式: eclipse插件安装地址:http://mybatis.google ...

  6. 五:Edits Viewer离线日志查看器

    离线日志查看器可以将二进制日志翻译成可读的文件(如XML),只有当hadoop集群停止时才能使用.输入文件支持的类型:XML和二进制.输出文件支持类型:XML 二进制 Stats(标准输出?)     ...

  7. POJ 3384 Feng Shui(计算几何の半平面交+最远点对)

    Description Feng shui is the ancient Chinese practice of placement and arrangement of space to achie ...

  8. /etc/fstab 文件如何填写(转)

    转载自 http://hi.baidu.com/jingzhongchen/blog/item/8e6f552dcead7ce98b139952.html 看你对/etc/fstab文件了解多少?   ...

  9. 11.24Daily Scrum

    人员 任务分配完成情况 明天任务分配 王皓南 实现网页上视频浏览的功能.研究相关的代码和功能.990 测试 申开亮 实现网页上视频浏览的功能.研究相关的代码和功能.991 测试 王宇杰 负责后台代码测 ...

  10. udf.dll 源码

    一点关于UDF的发散思路 Author:mer4en7y Team:90sec 声明:UDF源码作者langouster 相信各位牛对UDF都不会陌生,看论坛叶总共享了一份UDF源码,以前一直没看过, ...