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. iOS9 UI Tests探索笔记

    UI Tests是什么? UI Tests是一个自动测试UI与交互的Testing组件 UI Tests有什么用? 它可以通过编写代码.或者是记录开发者的操作过程并代码化,来实现自动点击某个按钮.视图 ...

  2. codeforces 577B. Modulo Sum 解题报告

    题目链接:http://codeforces.com/problemset/problem/577/B 题目意思:就是给出 n 个数(a1, a2, ..., an) 和 m,问能不能从这 n 个数中 ...

  3. WP8图片缩放功能实现

    最近在学习WP8,想实现WP微信中查看图片时的放大缩小功能. 网上找了两个解决方案: 1 利用GestureListener 这个类在Microsoft.Phone.Controls.Toolkit中 ...

  4. Android屏幕适配dp、px两套解决办法

    "又是屏幕适配,这类文章网上不是很多了吗?" 我也很遗憾,确实又是老问题.但本文重点对网上的各种方案做一个简短的总结,和具体使用方法. 若想了解具体Android设备适配的前世因果 ...

  5. 【leetcode】Number of 1 Bits (easy)

    做太多遍了,秒杀. class Solution { public: int hammingWeight(uint32_t n) { ; ), num++); return num; } };

  6. MyEclipse8.5破解方法

    本文是转自其它博文,用以留着备份的~ Step: 1.建立一个任意名称的Java Project 2.在该工程中建立一个名文MyEclipseGen的Java文件(MyEclipseGen.java) ...

  7. Struts2之类型转换器

    一.类型转换器的应用场景 类型转换是OGNL的一部分,默认的八种基本类型.String.Date会使用类型转换,但是更复杂的类型转换就需要我们自定义了(虽然这个东西一般根本用不到),OGNL可以应用在 ...

  8. Delphi函数指针

    参考:http://blog.chinaunix.net/uid-91034-id-2009700.html http://blog.csdn.net/procedure1984/article/de ...

  9. Delphi基础语法的学习笔记和注意事项总结

    以下是我在自学Delphi的时候,对一些注意点的简单总结,并没有什么系统性可言,只是一个学习时顺手记下的笔记,主要为了当时加深对知识的印象,并没有希望能在以后的复习和使用Delphi中有什么多大的参考 ...

  10. SQL Server 2014 BI新特性(三)Power Query和Power Map功能预览

    Power Query和Power Map是微软前不久在WPC上发布的Power BI中新的针对Excel的功能.借助这两样功能,自助式BI将更方便你发现和处理数据并且丰富数据的可视化功能. Powe ...