C Looooops
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18808 | Accepted: 4928 |
Description
A Compiler Mystery: We are given a C-language style for loop of type
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.
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.
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
这道题实际上需要一定计算机组成原理的知识。题目中给出了数字可以取到的最大值即1<<k。而数字溢出之后就相当于是对(1<<k)进行取模,使得数字重新回到0~(1<<k)-1的范围之内。因而要对(1<<k)取模。所以我们所求解的就转变成了(A+k*C)%(1<<k)=B。这样就可以用扩展欧几里德算法进行计算。将式子转换成k1*C-k2*(1<<k)=(B-A)。然后进行判断。还需要注意k最大取到32,如果只是(1<<k)的话,当k=32的时候,1<<32=1。所以要用(1LL<<k)来表示。防止int的溢出。用long long防止溢出。
参考代码:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<ctime>
#include<cstdlib>
#include<iomanip>
#include<utility>
#define pb push_back
#define mp make_pair
#define CLR(x) memset(x,0,sizeof(x))
#define _CLR(x) memset(x,-1,sizeof(x))
#define REP(i,n) for(int i=0;i<n;i++)
#define Debug(x) cout<<#x<<"="<<x<<" "<<endl
#define REP(i,l,r) for(int i=l;i<=r;i++)
#define rep(i,l,r) for(int i=l;i<r;i++)
#define RREP(i,l,r) for(int i=l;i>=r;i--)
#define rrep(i,l,r) for(int i=1;i>r;i--)
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
using namespace std;
ll a,b,c,k;
void exgcd(ll a,ll b,ll &d,ll &x,ll &y)
{
if(!b)
{
x=1,y=0,d=a;
return;
}
exgcd(b,a%b,d,y,x);
y-=(a/b)*x;
}
int main()
{
while(~scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k)&&a+b+c+k)
{
ll f=(1LL<<k),d,x,y;
exgcd(c,f,d,x,y);
if((b-a)%d) printf("FOREVER\n");
else
{
x*=((b-a)/d);
ll n1=f/d;
x=(x%n1+n1)%n1;
printf("%I64d\n",x);
}
}
}