D. C Looooops(扩欧)

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.

Samples
Input 复制
3 3 2 16
3 7 2 16
7 3 2 16
3 4 2 16
0 0 0 0
Output
0
2
32766
FOREVER
Hint
CTU Open 2004, POJ 2142

题意就是for(i=a;i!=b;i+=c) 每次对 2 k 2^k 2k取膜
思路:
看着有点复杂,简单的想就是这一个式子:
c x = ( b − a ) c^x=(b-a) cx=(ba)% 2 k 2^k 2k

#include <map>
#include <queue>
#include <string>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include <algorithm>
#include <math.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
typedef pair<ll,ll> pii;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mem(a,x) memset(a,x,sizeof(a))
#define debug(x) cout << #x << ": " << x << endl;
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<=(b);++i)
#define repr(i,b,a) for(int i=int(b);i>=(a);--i)
const int maxn=1000+10;
#define inf 0x3f3f3f3f
#define sf scanf
#define pf printf
const int mod=1e9+7;
const int MOD=1e9+7;

inline int read() {
    int x=0;
    bool t=false;
    char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    if(ch=='-')t=true,ch=getchar();
    while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
    return t?-x:x;
}

ll gcd(ll a,ll b) {
    while(b) {
        ll tmp=a%b;
        a=b;
        b=tmp;
    }
    return a;
}
ll exgcd(ll a,ll b,ll &x,ll &y) { //扩欧
    if(b==0) {
        x=1,y=0;
        return a;
    }
    ll d=exgcd(b,a%b,y,x);
    y=y-a/b*x;
    return d;
}
ll a,b,c,k;
int main() {
    /// c * x  =  (b - a) % (2 ^ k)
    while(cin>>a>>b>>c>>k&&(a||b||c||k)){
        ll x,y;
        ll maxx=(1ll<<k);
        ll d=exgcd(c,maxx,x,y);
        ll e=b-a;
        if(e%d){
            puts("FOREVER");
        }else {
            ll r=maxx/d;
            x=(x*e/d%r+r)%r;
            printf("%lld\n",x);
        }

    }
    return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值