C Looooops

Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u

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 < 2 k) modulo 2 k.

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 < 2 k) 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

题意:for(i=A;i!=B;i+=C){i%(2^k)};问你循环执行几次?

思路:先假设等式成立:(A+x*C)%(2^k)=B

变形(2^k)*y+B=A+C*x ==> C*x+(-(2^k)*y)=B-A;

ax+by=c

所以现在你知道怎么做了吧。哈哈!

转载请注明出处:http://www.cnblogs.com/yuyixingkong/

题目链接:http://poj.org/problem?id=2115

#include<stdio.h>
#define LL unsigned long long
void exgcd(LL a,LL b,LL& d,LL& x,LL& y)
{
if(!b){d=a;x=;y=;}
else
{
exgcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
int main()
{
LL A,B,C,k;
while(scanf("%llu%llu%llu%llu",&A,&B,&C,&k),(A+B+C+k))
{
LL a,b,c,d,x,y,dm;
c=B-A;
if(c==){printf("0\n");continue;}
a=C;
b=(LL)<<k;
exgcd(a,b,d,x,y);
if(c%d){ printf("FOREVER\n");continue;}
dm=b/d;
x=(((x*c/d)%dm)+dm)%dm; printf("%llu\n",x);
}
return ;
}

C Looooops(poj2115+扩展欧几里德)的更多相关文章

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

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

  2. poj2115[扩展欧几里德]

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22260   Accepted: 6125 Descr ...

  3. POJ2115 C Looooops 扩展欧几里德

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

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

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

  5. poj 2115 C Looooops 扩展欧几里德

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

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

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

  7. poj2142-The Balance(扩展欧几里德算法)

    一,题意: 有两个类型的砝码,质量分别为a,b;现在要求称出质量为d的物品, 要用多少a砝码(x)和多少b砝码(y),使得(x+y)最小.(注意:砝码位置有左右之分). 二,思路: 1,砝码有左右位置 ...

  8. (扩展欧几里德算法)zzuoj 10402: C.机器人

    10402: C.机器人 Description Dr. Kong 设计的机器人卡尔非常活泼,既能原地蹦,又能跳远.由于受软硬件设计所限,机器人卡尔只能定点跳远.若机器人站在(X,Y)位置,它可以原地 ...

  9. [BZOJ1407][NOI2002]Savage(扩展欧几里德)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1407 分析: m,n范围都不大,所以可以考虑枚举 先枚举m,然后判定某个m行不行 某个 ...

随机推荐

  1. dom4j 通过 org.dom4j.XPath 设置命名空间来支持 带namespace 的 xpath

    测试文件 test.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/ ...

  2. Crontab定时执行Oracle存储过程

    Crontab定时执行Oracle存储过程 需求描述 我们有一个Oracle的存储过程,里面是每个月需要执行一下,生成报表,然后发送给业务部门,这一个功能我们有实现在系统的前台界面(如图1-1),但是 ...

  3. Vuejs——(11)组件——slot内容分发

    版权声明:出处http://blog.csdn.net/qq20004604   目录(?)[+]   本篇资料来于官方文档: http://cn.vuejs.org/guide/components ...

  4. 关于使用Visual编译静态库动态库及其使用的问题

    本文主要讲述了如何使用Visual Studio 2013 编译静态库和动态库,并使用. 一.静态库 1.  编写静态库 若要创建将引用并使用刚创建的静态库的应用程序,请从“文件”菜单中选择“新建”, ...

  5. AJPFX:什么是外汇交易

    外汇交易是对货币对的一种买卖,是以一个国家货币与另外一个国家货币进行交换,即您在买入一国货币的同时,您也卖出了另一国的货币.所以在外汇市场上,人们的交易对象就是“货币对“,比如欧元/美元,美元/日元, ...

  6. dubbo rpc调用,接收到的bean为null原因?

    前几天对接公司内部其他部门的系统,用dubbo调用,dubbo看起来很简单,但是却让我们调试了好久啊! 下面是调试纪录: 1. 调用该服务时,直接调不通,查看错误为 no provider ? 然后就 ...

  7. ElasticSearch权威指南学习(结构化查询)

    请求体查询 简单查询语句(lite)是一种有效的命令行adhoc查询.但是,如果你想要善用搜索,你必须使用请求体查询(request body search)API. 空查询 我们以最简单的 sear ...

  8. Maven(个人整理)(一,未完待续)

                                                      Maven 1. 什么是Maven? 乍一看,Maven看起来很多东西,但简而言之,Maven试图将 ...

  9. webpack通过postcss-loader添加浏览器前缀

    在webpack中,我们可以很方便的使用autoprefixer来为css3属性添加不同的浏览器前缀. 首先,需要安装autoprefixer不用多说了,其次是安装postcss-loader(npm ...

  10. Virtualbox+Vagrant环境准备

    环境准备 所需软件 下载地址 VirtualBox-5.2.8-121009-Win https://download.virtualbox.org/virtualbox/5.2.12/Virtual ...