Problem B

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 6   Accepted Submission(s) : 3
Problem 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
 
 #include<iostream>
//#include<cstdio>
using namespace std;
long long x,y,q;
void exgcd( long long a,long long b)
{
if(b==)
{
x=,y=,q=a;
}
else
{
exgcd(b,a%b);
long long temp=x;
x=y,y=temp-a/b*y;
}
} int main()
{
long long a,b,c,k;
long long sum;
while(cin>>a>>b>>c>>k,a+b+c+k)
{
//sum=1<<k;
sum=;
for(int i=;i<k;i++)
{
sum*=;
}
exgcd(c,sum);
q;
if((b-a)%q)
{
cout<<"FOREVER"<<endl;
}
else
{
long long temp=sum/q;
cout<<(((b-a)/q*x%temp+temp)%temp)<<endl;
}
}
return ;
}

HDUOJ----专题训练的更多相关文章

  1. DP专题训练之HDU 2955 Robberies

    打算专题训练下DP,做一道帖一道吧~~现在的代码风格完全变了~~大概是懒了.所以.将就着看吧~哈哈 Description The aspiring Roy the Robber has seen a ...

  2. dp专题训练

    ****************************************************************************************** 动态规划 专题训练 ...

  3. bryce1010专题训练——LCT&&树链剖分

    LCT&&树链剖分专题 参考: https://blog.csdn.net/forever_wjs/article/details/52116682

  4. DP专题训练之HDU 1087 Super Jumping!

    Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is ve ...

  5. DP专题训练之HDU 1506 Largest Rectangle in a Histogram

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  6. DP专题训练之HDU 1231 最大连续子序列

    Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j < ...

  7. DP专题训练之HDU 1864 最大报销额

    做DP一定要注意数组的大小,嗯,就是这样~ Description 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求每张发票的总额不得超过10 ...

  8. bzoj专题训练

    //http://blog.csdn.net/PoPoQQQ/article/category/2542243

  9. 算法专题训练 搜索a-T3 Ni骑士(ni)

    搞了半天八数码弄不出来就只好来打题解  这道题是在搜索a碰到的(链接: http://pan.baidu.com/s/1jG9rQsQ ) 感觉题目最大亮点就是这英文简写"ni", ...

  10. 图论专题训练1-D(K步最短路,矩阵连乘)

    题目链接 /* *题目大意: *求出从i到j,刚好经过k条边的最短路; * *矩阵乘法的应用之一(国家队论文): *矩阵乘法不满足交换律,矩阵乘法满足结合律; *给定一个有向图,问从A点恰好走k步(允 ...

随机推荐

  1. dao层的泛型实现(2种方法)

    一: package com.wzs.test2.dao; import java.util.List; public interface CommonDAO { public <T> v ...

  2. 从PSD到HTML,网页的实现

    在学习完<From PSD to HTML: Building a Set of Website Designs Step by Step>之后,使我对网页的设计和实现有了更深入的认识,我 ...

  3. 跟我学Spring3(9.2):Spring的事务之事务管理器

    原文出处: 张开涛9.2.1 概述 Spring框架支持事务管理的核心是事务管理器抽象,对于不同的数据访问框架(如Hibernate)通过实现策略接口PlatformTransactionManage ...

  4. coursera课程Text Retrieval and Search Engines之Week 1 Overview

    Week 1 OverviewHelp Center Week 1 On this page: Instructional Activities Time Goals and Objectives K ...

  5. 【BZOJ】【3239】Discrete Logging

    BSGS BSGS裸题,嗯题目中也有提示:求a^m (mod p)的逆元可用快速幂,即 pow(a,P-m-1,P) * (a^m) = 1 (mod p) /******************** ...

  6. iOS开发-UINavigationController简单介绍

    导航条或者说导航栏目现在在App中基本上也算是标配,类似于父子级别的味道在里面,UINavigationController就是负责简化这一实现功能的,属于iOS开发中比较常用的一种容器View co ...

  7. 动态装载外部JavaScript脚本文件

    当我们请求一个URL地址时,浏览器会从远程服务器装载各种所需的资源,如JavaScript.CSS.图片等.而在加载JavaScript时,常常会发生下面这种情况: 也就是说,当浏览器碰到Script ...

  8. 【Python】Python 微服务框架 nameko

    nameko: 1.支持服务发现.负载均衡 2.支持依赖自动注入,使用很方便 3.缺点:超时.限速.权限等机制不完善 代码示例:https://github.com/junneyang/nameko- ...

  9. 虚拟机配置Cognos报错CFG-ERR-0106

    在虚拟机中安装Cognos 之后,启动了好多次,都启动失败,如下图所示,错误如下图所示 已确保已下信息设置正确 1:内容库配置OK 2:Java_home OK 3:字符集OK ----------- ...

  10. C++的四种初始化形式以及类型转换

      C++中有如下的方式来初始化一个变量. 但当进行类型转换时,只有两种方式可用,其他两种方式会报错.