poj 2115 C Looooops 数论

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>
__int64 extend_gcd(__int64 a,__int64 b,__int64& x,__int64& y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    __int64 d=extend_gcd(b,a%b,x,y);
    __int64 xt=x;
    x=y;
    y=xt-a/b*y;
    return d;
}
int main(void)
{
    __int64 A,B,C,k;
    __int64 a,b,n,x,y,d;
    while(scanf("%I64d %I64d %I64d %I64d",&A,&B,&C,&k))
    {
        if(!A && !B && !C && !k)
            break;

         a=C;
         b=B-A;
        n=(__int64)1<<k;
         d=extend_gcd(a,n,x,y);

        if(b%d!=0)
            printf("FOREVER\n");
        else
        {
            x=(x*(b/d))%n;
            x=(x%(n/d)+n/d)%(n/d);
            printf("%I64d\n",x);
        }
    }
    return 0;
}

x=[ (B-A+2^k)%2^k ]/C

C*X=(B-A+2^k)%2^k

一开始 方程不知道咋办 后来 看到别人统一一下变量 顿时 愚蠢的我感觉清晰了一点点 

令a=C, b=B-A, MOD=2^k;

方程变成了 ax=(n+MOD)%MOD;

看到某个大神贴的一张图片很给力 我也贴一下,顺便贴一下他的解析过程,很容易懂得,一开始学数论 不可能自己完全推出来 都是要看要做,做多了应该就行了把,我天赋比较差的,求保佑:!


该方程有解的充要条件为 gcd(a,n) | b ,即 b% gcd(a,n)==0

令d=gcd(a,n)

有该方程的 最小整数解为 x = e (mod n/d)

其中e = [x0 mod(n/d) + n/d] mod (n/d) ,x0为方程的最小解

那么原题就是要计算b% gcd(a,n)是否为0,若为0则计算最小整数解,否则输出FOREVER

 

当有解时,关键在于计算最大公约数 d=gcd(a,n) 与 最小解x0

参考《算法导论》,引入欧几里得扩展方程  d=ax+by ,

通过EXTENDED_EUCLID算法(P571)求得d、x、y值,其中返回的x就是最小解x0,求d的原理是辗转相除法(欧几里德算法)

再利用MODULAR-LINEAR-EQUATION-SOLVER算法(P564)通过x0计算x值。注意x0可能为负,因此要先 + n/d 再模n/d。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值