C Looooops
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23616   Accepted: 6517

Description

A Compiler Mystery: We are given a C-language style for loop of type

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 consists of several instances. Each instance is described by a single line with four integers A, B, C, k separated by a single space. The integer k (1 <= k <= 32) is the number of bits of the control variable of the loop and A, B, C (0 <= A, B, C < 2k) are the parameters of the loop.

The input is finished by a line containing four zeros.

Output

The output consists of several lines corresponding to the instances on the input. The i-th line contains either the number of executions of the statement in the i-th instance (a single integer number) or the word FOREVER if the loop does not terminate.

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+bx)%(2^k)==c,求x最小值;
思路:解扩展欧几里德;

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define ll long long
#define esp 1e-13
const int N=1e4+,M=1e6+,inf=1e9+,mod=;
void extend_Euclid(ll a, ll b, ll &x, ll &y)
{
if(b == )
{
x = ;
y = ;
return;
}
extend_Euclid(b, a % b, x, y);
ll tmp = x;
x = y;
y = tmp - (a / b) * y;
}
ll gcd(ll a,ll b)
{
if(b==)
return a;
return gcd(b,a%b);
}
ll pow1(ll x)
{
ll sum=;
for(ll i=;i<x;i++)
sum*=;
return sum;
}
int main()
{
ll x,y,i,z,t;
while(~scanf("%lld%lld%lld%lld",&x,&y,&i,&t))
{
if(x==&&y==&&i==&&t==)
break;
ll m=pow1(t);
ll c=((y-x)%m+m)%m;
ll j,k;
if(c%gcd(m,i)==)
{
extend_Euclid(i,m,j,k);
ll ans=j*(c/gcd(m,i));
m=m/gcd(m,i);
printf("%lld\n",(ans%m+m)%m);
}
else
printf("FOREVER\n");
}
return ;
}

poj 2115 C Looooops 扩展欧几里德的更多相关文章

  1. POJ 2115 C Looooops (扩展欧几里德 + 线性同余方程)

    分析:这个题主要考察的是对线性同余方程的理解,根据题目中给出的a,b,c,d,不难的出这样的式子,(a+k*c) % (1<<d) = b; 题目要求我们在有解的情况下求出最小的解,我们转 ...

  2. 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( ...

  3. POJ 2115 C Looooops(扩展欧几里得)

    辗转相除法(欧几里得算法) 时间复杂度:在O(logmax(a, b))以内 int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a ...

  4. POJ 2115 C Looooops扩展欧几里得

    题意不难理解,看了后就能得出下列式子: (A+C*x-B)mod(2^k)=0 即(C*x)mod(2^k)=(B-A)mod(2^k) 利用模线性方程(线性同余方程)即可求解 模板直达车 #incl ...

  5. poj 2115 C Looooops(扩展gcd)

    题目链接 这个题犯了两个小错误,感觉没错,结果怒交了20+遍,各种改看别人题解,感觉思路没有错误,就是wa. 后来看diccuss和自己查错,发现自己的ecgcd里的x*(a/b)写成了x*a/b.还 ...

  6. POJ - 2115 C Looooops(扩展欧几里德求解模线性方程(线性同余方程))

    d.对于这个循环, for (variable = A; variable != B; variable += C) statement; 给出A,B,C,求在k位存储系统下的循环次数. 例如k=4时 ...

  7. poj2115-C Looooops(扩展欧几里德算法)

    本题和poj1061青蛙问题同属一类,都运用到扩展欧几里德算法,可以参考poj1061,解题思路步骤基本都一样.一,题意: 对于for(i=A ; i!=B ;i+=C)循环语句,问在k位存储系统中循 ...

  8. POJ2115 C Looooops 扩展欧几里德

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

  9. poj2115 Looooops 扩展欧几里德的应用

    好开心又做出一道,看样子做数论一定要先看书,认认真真仔仔细细的看一下各种重要的性质 及其用途,然后第一次接触的题目 边想边看别人的怎么做的,这样做出第一道题目后,后面的题目就完全可以自己思考啦 设要+ ...

随机推荐

  1. Cocos2d-x Lua中实例:特效演示

    下面我们通过一个实例介绍几个特效的使用,这个实例如下图所示,上图是一个操作菜单场景,选择菜单可以进入到下图动作场景,在下图动作场景中点击Go按钮可以执行我们选择的特性动作,点击Back按钮可以返回到菜 ...

  2. 爬虫实战【6】Ajax内容解析-今日头条图集

    Ajax技术 AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). Ajax并不是新的编程语言,而是一种使用现有标准的新方法,当然 ...

  3. 巨蟒python全栈开发django10:ajax&&登录认证

    通过题目进行知识点回顾: 聚合查询 From django.db.models import Avg,Min,Max,F,Q,Count,Sum #查询书籍的平均值 Ret= Models.Book. ...

  4. 基于Mesos和Docker的分布式计算平台

    基于Mesos和Docker的分布式计算平台 http://www.csdn.net/article/2015-06-09/2824906

  5. Linux下修改密码复杂度

    在linux,设置密码复杂度的方法有几个1. 一个是在/etc/login.defs文件,里面几个选项PASS_MAX_DAYS 90 #密码最长过期天数PASS_MIN_DAYS 80 #密码最小过 ...

  6. ajax跨域资源共享

    一.同域发送数据 略 二.跨域发送数据 1.存在的问题 1.什么是同源策略 同源策略阻止从一个域名上加载的脚本获取或操作另一个域名上的文档属性.也就是说,受到请求的 URL 的域名必须与当前 Web ...

  7. ASP.NET,什么是MVC,MVC的简单介绍

    什么是MVC模式 MVC(Model-View-Controller,模型—视图—控制器模式)用于表示一种软件架构模式.它把软件系统分为三个基本部分:模型(Model),视图(View)和控制器(Co ...

  8. WebMvcRegistrationsAdapter

    27.1.1. Spring MVC自动配置 如果希望使用自定义的RequestMappingHandlerMapping,RequestMappingHandlerAdapter,或Exceptio ...

  9. django 查询

    mail = UserProfile.objects.get(email = email) get如果没有查询到会抛出一个不存在的异常                                 ...

  10. sql server分区

    1. 创建分区 分区步骤:1.创建分区函数  2.创建分区架构 3.创建分区索引(聚集)   --1. 创建分区函数 DECLARE @dt datetime SET @dt = '20030901' ...