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 < 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.

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

思路:

题目搁了两天,开始想了个很复杂的方法,花了好长时间调试结果TLE

后来实在没办法去搜了一下,才知道是用同余方程 解决

根据题目可以列出一个方程式,将加c的次数设成x:

∵   a+cx≡b(%mod);

∴  a+c*x+mod*y=b;

∴  c*x+mod*y=b-a;

根据exgcd可以求解  同时好要求出最小整数x的解就是结果

代码:

#include<stdio.h>
typedef long long LL;
LL GCD;
LL exgcd(LL a,LL b,LL &x,LL &y) // 拓展欧几里得求x、y特解
{
if(!b){
x=1;y=0;return a;
}
GCD=exgcd(b,a%b,y,x);
y-=a/b*x;
return GCD;
}
LL qpow(LL c,LL q) // 快速幂 因为题目涉及求2^k 如果用pow可能会出错
{
LL ans=1; //快速幂里面不需要%mod 和mod没有关系 不需要担心快速幂结果会超过2^k
while(q){
if(q%2){
ans*=c;
}
c*=c;
q/=2;
}
return ans;
}
int main()
{
LL a,b,c,k,x,y,t;
while(scanf("%lld%lld%lld%lld",&a,&b,&c,&k)!=EOF){
if(a==0&&b==0&&c==0&&k==0){
break;
}
else{
GCD=exgcd(c,qpow(2,k),x,y); //将 c、mod=2^k、x、y 依次带入拓展欧几里得方程求解
if((b-a)%GCD){
printf("FOREVER\n"); // 判断方程是否有解
}
else{
x*=((b-a)/GCD); //类似求解ax+by=c最小整数解的方法 得出最小x的值
t=qpow(2,k)/GCD; //因为求x所以t=b/GCD 但这个b并不是输入的b 而是 方程里面对应
if(t<0){ 的b 即 qpow(2,k)
t=-t;
}
x=(x%t+t)%t;
printf("%lld\n",x); //输出结果x
}
}
}
return 0;
}

Looooops(求解同余方程、同余方程用法)【拓展欧几里得】的更多相关文章

  1. 【lydsy1407】拓展欧几里得求解不定方程+同余方程

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1407 题意: 有n个野人,野人各自住在第c[i]个山洞中(山洞成环状),每年向前走p[i] ...

  2. [POJ2115]C Looooops 拓展欧几里得

    原题入口 这个题要找到本身的模型就行了 a+c*x=b(mod 2k) ->  c*x+2k*y=b-a 求这个方程对于x,y有没有整数解. 这个只要学过拓展欧几里得(好像有的叫扩展欧几里德QA ...

  3. ACM数论-欧几里得与拓展欧几里得

    ACM数论——欧几里得与拓展欧几里得 欧几里得算法: 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd ...

  4. Vulnerable Kerbals CodeForces - 772C【拓展欧几里得建图+DAG上求最长路】

    根据拓展欧几里得对于同余方程 $ax+by=c$ ,有解的条件是 $(a,b)|c$. 那么对于构造的序列的数,前一个数 $a$  和后一个数 $b$ ,应该满足 $a*x=b(mod m)$ 即 $ ...

  5. HDU-3579-Hello Kiki (利用拓展欧几里得求同余方程组)

    设 ans 为满足前 n - 1个同余方程的解,lcm是前n - 1个同余方程模的最小公倍数,求前n个同余方程组的解的过程如下: ①设lcm * x + ans为前n个同余方程组的解,lcm * x ...

  6. POJ 2891 Strange Way to Express Integers(拓展欧几里得)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  7. BZOJ-1407 Savage 枚举+拓展欧几里得(+中国剩余定理??)

    zky学长实力ACM赛制测试,和 大新闻(YveH) 和 华莱士(hjxcpg) 组队...2h 10T,开始 分工我搞A,大新闻B,华莱士C,于是开搞: 然而第一题巨鬼畜,想了40min发现似乎不可 ...

  8. [zoj 3774]Power of Fibonacci 数论(二次剩余 拓展欧几里得 等比数列求和)

    Power of Fibonacci Time Limit: 5 Seconds      Memory Limit: 65536 KB In mathematics, Fibonacci numbe ...

  9. 51 Nod 1256 乘法逆元(数论:拓展欧几里得)

    1256 乘法逆元  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K ...

随机推荐

  1. sklearn学习:为什么roc_auc_score()和auc()有不同的结果?

    为什么roc_auc_score()和auc()有不同的结果? auc():计算ROC曲线下的面积.即图中的area roc_auc_score():计算AUC的值,即输出的AUC 最佳答案 AUC并 ...

  2. html5拖动监听

    在拖动目标上触发事件 (源元素): ondragstart - 用户开始拖动元素时触发 ondrag - 元素正在拖动时触发 ondragend - 用户完成元素拖动后触发 释放目标时触发的事件: o ...

  3. Java线程的启动与中止

    一.线程与进程的关系 关于进程与线程,百度百科上是这样描述的: 进程(Process) 是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础. 在当 ...

  4. vue中使用vue-qrcode生成二维码

    要使用二维码,引入一个包就可以了,使用非常简单,话不多说,看代码吧 //1,引入, import VueQrcode from '@xkeshi/vue-qrcode'; Vue.component( ...

  5. BFC与优雅降级 渐进增强——学习笔记

    BFC(块级格式化上下文) BFC(Block formatting context) 直译为"块级格式化上下文". 元素的显示模式 我们前面讲过 元素的显示模式 display. ...

  6. abp(net core)+easyui+efcore实现仓储管理系统——出库管理之一(四十九)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

  7. 12 . Python3之网络编程

    互联网的本质 两台计算机之间的通信与两个人打电话原理是一样的. # 1. 首先要通过各种物理连接介质连接 # 2. 找准确对方计算机(准确到软件)的位置 # 3. 通过统一的标准(一般子协议)进行数据 ...

  8. Spring boot Sample 007之spring-boot-log4j2

    一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 spring boot 整合多环境log4j2 三.步骤 3.1.点击File -> New Project -> ...

  9. JAVASE(十六) IO流 :File类、节点流、缓冲流、转换流、编码集、对象流

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 1.File类型 1.1.File类的理解 File类是在java.io包下 File可以理解成一个文件 ...

  10. 高性能可扩展mysql 笔记(一)数据库表、索引、SQL语句设计规范

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 项目说明:该笔记的背景为电商平台项目,电商项目由于其高并发.多线程.高耗能等特性,在众多的项目类型中涉及 ...