zoukankan      html  css  js  c++  java
  • POJ2115:C Looooops——题解

    http://poj.org/problem?id=2115

    题目大意:for(i=A;i!=B;i+=C),i的类型的范围为0<=a<1<<k

    exgcd裸题目。

    设a=C,b=(1<<k),c=(B-A).

    则ax+by=c.

    #include<cstdio>
    #include<cctype>
    #include<iostream>
    using namespace std;
    typedef long long ll;
    ll gcd(ll a,ll b){
        return b?gcd(b,a%b):a;
    }
    void exgcd(ll a,ll b,ll &x,ll &y){
        if(b==0){
        x=1;y=0;
        return;
        }
        exgcd(b,a%b,x,y);
        ll temp;
        temp=x;
        x=y;
        y=temp-(a/b)*y;
        return;
    }
    int main(){
        ll A,B,C,k;
        while(cin>>A>>B>>C>>k){
        if(A==B&&B==C&&C==k&&A==0)return 0;
        ll x,y;
        ll a=C,b=(ll)1<<k,c=B-A;
        if(!c){
            printf("0
    ");
            continue;
        }
        ll g=gcd(a,b);
        if(c%g){
            printf("FOREVER
    ");
            continue;
        }
        a/=g;b/=g;c/=g;
        exgcd(a,b,x,y);
        x=(x%b*c%b+b)%b;
        printf("%lld
    ",x);
        }
        return 0;
    }

    #include<cstdio>
    #include<cctype>
    #include<iostream>
    using namespace std;
    typedef long long ll;
    ll gcd(ll a,ll b){
        return b?gcd(b,a%b):a;
    }
    void exgcd(ll a,ll b,ll &x,ll &y){
        if(b==0){
        x=1;y=0;
        return;
        }
        exgcd(b,a%b,x,y);
        ll temp;
        temp=x;
        x=y;
        y=temp-(a/b)*y;
        return;
    }
    int main(){
        ll A,B,C,k;
        while(cin>>A>>B>>C>>k){
        if(A==B&&B==C&&C==k&&A==0)return 0;
        ll x,y;
        ll a=C,b=(ll)1<<k,c=B-A;
        if(!c){
            printf("0 ");
            continue;
        }
        ll g=gcd(a,b);
        if(c%g){
            printf("FOREVER ");
            continue;
        }
        a/=g;b/=g;c/=g;
        exgcd(a,b,x,y);
        x=(x%b*c%b+b)%b;
        printf("%lld ",x);
        }
        return 0;
    }

  • 相关阅读:
    HtmlAgilityPack
    随笔-20150513
    过滤掉html 标签
    json转换
    第28月第3天 c语言读写文件
    第27月第28天 iOS bundle
    第27月第27天 https
    第27月第25天 clang -rewrite-objc main.m
    第27月第24天 git pull fetch
    第27月第18天 epoll lt et
  • 原文地址:https://www.cnblogs.com/luyouqi233/p/7904472.html
Copyright © 2011-2022 走看看