C Looooops
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23637   Accepted: 6528

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+cx%2^k=b,求解最小的x
a-b+cx%2^k=0
cx=(b-a)%2^k·························@1:典型的同余方程,通过ext_gcd求解
cx+(2^k)y=(b-a)
当且仅当 gcd(c,2^k)|(b-a)时,方程有解。
我们通过ext_gcd求得 cx+(2^k)y=gcd(b-a)的解 x,y,gcd.
把方程的两边同时/gcd*(b-a)即得到 @1式的解。
即 x = x*(b-a)/gcd。
因为我们要求最小的x,所以我们求出符合条件的x的变化周期:T:= (2^k)/gcd.
然后通过(x%T+T)%T得到最小的x,别问我为什么,因为我也不知道为什么。
#include<iostream>
#include<stdio.h>
using namespace std;
long long pow(long long k)
{
long long ans=;
for(int i=;i<k;i++)
ans*=;
return ans;
}
long long ext_gcd(long long a,long long b,long long *x,long long *y)
{
if(b==)
{
*x=,*y=;
return a;
}
long long r = ext_gcd(b,a%b,x,y);
long long t = *x;
*x = *y;
*y = t - a/b * *y;
return r;
}
int main()
{
long long a,b,c,k;
while(~scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k))
{
if((a+b+c+k)==) break;
long long x,y;
long long _gcd_ = ext_gcd(c,pow(k),&x,&y);
if((b-a)%_gcd_)
{
printf("FOREVER\n");
continue;
}
long long tmp_ans = x*(b-a)/_gcd_;
long long T = pow(k)/_gcd_;/*总结一下: b/gcd是 ax+by = k*gcd中,x*k/gcd的周期*/
long long ans = (tmp_ans%T+T)%T;
printf("%I64d\n",ans);
}
return ;
}

poj 2115 Looooops的更多相关文章

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

  2. 【题解】POJ 2115 C Looooops (Exgcd)

    POJ 2115:http://poj.org/problem?id=2115 思路 设循环T次 则要满足A≡(B+CT)(mod 2k) 可得 A=B+CT+m*2k 移项得C*T+2k*m=B-A ...

  3. POJ 2115 C Looooops(模线性方程)

    http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...

  4. POJ 2115 C Looooops(Exgcd)

    [题目链接] http://poj.org/problem?id=2115 [题目大意] 求for (variable = A; variable != B; variable += C)的循环次数, ...

  5. poj 2115 C Looooops——exgcd模板

    题目:http://poj.org/problem?id=2115 exgcd裸题.注意最后各种%b.注意打出正确的exgcd板子.就是别忘了/=g. #include<iostream> ...

  6. POJ 2115 C Looooops

    扩展GCD...一定要(1L<<k),不然k=31是会出错的 ....                        C Looooops Time Limit: 1000MS   Mem ...

  7. Poj 2115 C Looooops(exgcd变式)

    C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22704 Accepted: 6251 Descripti ...

  8. POJ 2115:C Looooops

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19536   Accepted: 5204 Descr ...

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

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

随机推荐

  1. Effective C++ -----条款12: 复制对象时勿忘其每一个成分

    Copying函数应该确保复制“对象内的所有成员变量”及“所有base class成分”. 不要尝试以某个copying函数实现另一个copying函数.应该将共同机能放进第三个函数中,并由两个cop ...

  2. Enum:Game of Lines(POJ 3668)

    画直线 题目大意:给定一些点集,要你找两点之间的连线不平行的有多少条 数据量比较少,直接暴力枚举,然后放到set查找即可 #include <iostream> #include < ...

  3. WebService及WCF获取客户端IP,端口

    wcf获取客户端IP,端口 var context = OperationContext.Current; var properties = context.IncomingMessageProper ...

  4. 【编程题目】在从 1 到 n 的正数中 1 出现的次数

    30.在从 1 到 n 的正数中 1 出现的次数(数组)题目:输入一个整数 n,求从 1 到 n 这 n 个整数的十进制表示中 1 出现的次数.例如输入 12,从 1 到 12 这些整数中包含 1 的 ...

  5. 非Unicode工程读取Unicode文件

    MyUnicodeReader.h #pragma once /******************************************************************** ...

  6. September 28th 2016 Week 40th Wednesday

    Love all, trust a few, do wrong to none. 爱所有人,信任一些人,不妨害任何人. Reading is a way for me to expand my min ...

  7. java获取短uuid

    public static String[] chars = new String[] { "a", "b", "c", "d&q ...

  8. 把Git Repository建到U盘上去(转)

    把Git Repository建到U盘上去 转 把Git Repository建到U盘上去 Git很火.原因有三: 它是大神Linus Torvalds的作品,天然地具备神二代的气质和品质: 促进了生 ...

  9. Swift - 初始化Initialization

    Ps:苹果官方文档-Initialization 自定义控件初始化中常见的几种错误(指定构造器和便利构造器)截图:   意思是:1.没有添加重写符override(重写父类方法)2.没有重写initW ...

  10. 用java来删除数组中指定的元素

    public static void main(String[] args){        String[] a = new String[]{"1","5" ...