POJ 2115 C Looooops(扩展欧几里德)

题意简述:给你四个数字a,b,c,k.其实就是一个for循环for(i = a; i != b ; i = (I+c)%2^k);求循环经过几次结束,输出次数;如果循环不结束,则输出"FOREVER".

一开始想到的是暴搜....但是k <= 32,所以余数也就是<=2^32-1;暴搜会超时啊、、后来听说这是扩展GCD的题目,就朝着这个方向想了想,但是只怪自己会的太少,以前学的时候学的不扎实,又忘记了啊、、、最后还是鹏哥给我讲的,感觉讲的很详细,在这里谢谢鹏哥了啊、、、

扩展GCD是原理:ax+by = c;令d = gcd(a,b),原方程有整数解当且仅当d|c,bx+(a%b)y = 1 <=> ax+b(x-[a/b]*y) = 1;

解二元模线性方程 算法步骤:

整数a,b,c,设d = gcd(a,b) 再用欧几里得算法求gcd(a,b)的过程中求方程ax+by = d的一组整数解:

若 b = 0, 则x = 1, y = 0;

否则,递归调用gcd(b, a%b),可以得到bx`+(a%b)y` = d;的解x`, y`,令x = y`, y = x-[a/b]*y`既满足ax+by=d.若d|c,则有a(kx)+b(ky) = c;否则原方程无整数解。

这道题可以列出方程:(a + c*x) %2^k = b;所以 a + c*x = b + y*2^k <=>c*x-y*2^k = b-a;符合扩展gcd的公式所以可以求出d = exit_gcd(c,2^k),如果d是(b-a)的倍数则输出几倍、、、否则输出“FOREVER”、、注意输出的正负问题。。

C Looooops
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15513 Accepted: 3960

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 < 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
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

long long x, y, f[40];

int exit_gcd(long long a, long long b)
{
    if(b == 0)
    {
        y = 0;
        x = 1;
        return a;
    }
    long long p = exit_gcd(b, a%b);
    long long temp = x;
    x = y;
    y = temp-(a/b)*y;
    return p;
}

void ff()
{
    int i;
    f[0] = 1;
    for(i = 1; i <= 33; i++)
        f[i] = f[i-1]*2;
}

int main()
{
    ff();
    long long a, b, c, k, n, d;
    while(cin >>a>>b>>c>>k)
    {
        if(!a && !b && !c && !k)
            break;
        n = f[k];
        if(a > b)
            b = ((b-a)%n+n)%n;
        else
            b = b-a;
        d = exit_gcd(c,n);
        if(b%d)
        {
            cout <<"FOREVER"<<endl;
            continue;
        }
        n = n/d;
        long long sum = (x*(b/d)%n+n)%n;
        cout <<sum<<endl;
    }
    return 0;
}


POJ2251是一个经典的题目,也被称为"水仙花的谜题"。该题目要求在一个三维的迷宫中找到从起点到终点的最短路径。 在这个题目中,迷宫由一个3D的数组表示,每个位置上的值代表了该位置的状态。其中,0表示可以通过的路径,1表示墙壁,2表示起点,3表示终点。你需要编写一个程序来找到从起点到终点的最短路径,并输出路径的长度。 解决这个问题的一种常见方法是使用广度优先搜索(BFS)算法。BFS算法可以从起点开始,逐层遍历迷宫中的位置,直到找到终点或者遍历完所有可达位置。在遍历过程中,你需要记录每个位置的距离和路径信息,以便找到最短路径。 以下是解决该问题的大致思路: 1. 定义一个队列,将起点加入队列,并标记起点已访问。 2. 使用循环来遍历队列中的元素,直到队列为空。 3. 在循环中,取出队列中的元素,并获取其相邻可达位置。 4. 对于每个相邻位置,判断是否为终点,如果是则输出最短路径长度并结束程序。 5. 如果不是终点,则判断该位置是否为可通过的路径,并且未被访问过。如果满足条件,则将该位置加入队列,并更新距离和路径信息。 6. 重复步骤2-5,直到找到终点或者遍历完所有可达位置。 这只是一个简单的介绍,实际解决该问题还需要考虑一些细节,比如如何表示迷宫、如何判断位置的合法性等。你可以在编写代码时参考相关的算法和数据结构知识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值