首页 > 其他 > 详细

poj 2115 C Looooops

时间:2017-08-09 16:24:19      阅读:246      评论:0      收藏:0      [点我收藏+]
C Looooops
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 28251   Accepted: 8079

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

/*
* @Author: Lyucheng
* @Date:   2017-08-07 08:57:28
* @Last Modified by:   Lyucheng
* @Last Modified time: 2017-08-09 16:10:54
*/
/*
 题意:给你一个简单的循环for(int i=A;i!=B;i+=C),i是16位得数,问你循环结束的次数,如果是死循环的话,就输出死循环

 思路:刚开始想简单的模拟一下,但是超时至少模拟2^k次循环,扩展欧几里得问题完美解决这个问题
    题意循环可以转化成公式 
        ( A + x*C ) mod (1<<K) = B 
    因为 题意中的A 肯定是小于(1<<k)的所以化简为
        x*C mod (1<<k) = B - A 
    这个就是扩展欧几里得定理求膜线性方程
    令D = B - A , n= (1<<k) 得
        x*C mod n = D
    则
        x*C - y*n =D 
    这就是
        a*x+b*y=c的扩展欧几里得定理解不定方程了
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

#define LL long long

using namespace std;

LL A,B,C,K;
LL x,y;
LL GCD;
LL tol;

void exgcd(LL a,LL b,LL &x,LL &y,LL &d)
{
    if(!b){ 
        d=a; x=1; y=0;
    }
    else {
        exgcd(b,a%b,y,x,d);
        y-=x*(a/b);
    }
}

int main(){ 
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    while(scanf("%I64d%I64d%I64d%I64d",&A,&B,&C,&K)!=EOF&&(A||B||C||K)){
        if(A==B){
            puts("0");
            continue;
        }
        B-=A;
        tol=(1LL<<K);
        exgcd(tol,C,x,y,GCD);
        if(B%GCD!=0){//无解
            puts("FOREVER");
            continue;
        }else{
            x*=(B/GCD);
            y*=(B/GCD);
            y=(y%(tol/GCD)+tol/GCD)%(tol/GCD);  
            printf("%I64d\n", y);  
        }
    }    
    return 0;
}

 

poj 2115 C Looooops

原文:http://www.cnblogs.com/wuwangchuxin0924/p/7325929.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!