zoukankan      html  css  js  c++  java
  • POJ 2115 C Looooops( 简单拓欧 + 快速幂 )


    **链接:****传送门 **

    题意:题目中给出一个循环 for (variable = A; variable != B; variable += C) ,这个东东还需要 mod 2^k 问至少多次能退出,如果进入死循环输出输出"FOREVER"

    思路:简单拓欧嘛,简单分析一下 A + C * x = B + 2^k * y,如果方程有解,那么最小整数解就是最少次数,否则就是死循环,写了一下快速幂,不清楚普通求 2^k 也并不会T,还是快一点好


    /*************************************************************************
        > File Name: poj2115.cpp
        > Author:    WArobot 
        > Blog:      http://www.cnblogs.com/WArobot/ 
        > Created Time: 2017年05月21日 星期日 20时06分05秒
     ************************************************************************/
    
    #include<bits/stdc++.h>
    using namespace std;
    
    #define ll long long
    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 , x , y );
    	ll tmp = x;
    	x = y;	 y = tmp - a/b*y;
    	return d;
    }
    ll quick_mod(ll a,ll x){
    	ll ret = 1;
    	while(x){
    		if( x & 1 ) ret = ret * a;
    		a = a * a;
    		x >>= 1;
    	}
    	return ret;
    }
    int main(){
    	ll a , b , c , k , x , y;
    	while(~scanf("%lld%lld%lld%lld",&a,&b,&c,&k)){
    		if( a == 0 && b == 0 && c == 0 && k == 0 ) break;
    		ll t = quick_mod(2,k);
    		ll d = exgcd( c , t , x , y );
    		ll c = b - a;
    		if( c % d != 0 )	printf("FOREVER
    ");
    		else{
    			x = x*(c/d);
    			x = (x%(t/d) + (t/d))%(t/d);
    			printf("%lld
    ",x);
    		}
    	}
    	return 0;
    }
  • 相关阅读:
    Abp Swagger API中文说明配置方法
    ABP框架中使用MySQL数据库
    windows + jenkins + .net core + iis自动化部署新手入门
    在图片上画矩形框
    base64转换成np、opencv、PIL
    RankSVM
    tf.placeholde函数解释与用法
    slim.arg_scope()的使用
    SSD网络结构
    tensorflow学习笔记
  • 原文地址:https://www.cnblogs.com/WArobot/p/6885849.html
Copyright © 2011-2022 走看看