很容易看出来一个同余式,说到底是解一个线性同余方程,计算机解通常有拓展欧几里得和欧拉定理两种算法,参照去年的NOIP水题,问题是这题数据范围是2^32所以要int64 TAT

#include<cstdio>

#include<iostream>

#include<string.h>

#include<math.h>

using namespace std;

__int64 exgcd(__int64 a,__int64 b,__int64&x,__int64 &y)

{

if(b==0)

{

x=1;y=0;return a;

}

else

{

__int64 r=exgcd(b,a %b,y,x);

y-=x*(a/b);

return r;

}

}

__int64 lme(__int64 a,__int64 b,__int64n)//ax=b(mod n)

{

__int64 x,y;

__int64 d=exgcd(a,n,x,y);

if(b%d!=0)return -1;

__int64 e=x*(b/d)%n+n;

return e%(n/d);

}

int main()

{

__int64 a,b,c,k;

scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k);

while(1)

{

__int64 d=lme(c,b-a,1LL<<k);

if (d==-1)

{

printf("FOREVER\n");

}

else

{

printf("%I64d\n",d);

}

scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k);

if(a==0 && b==0 && c==0 && k==0) break;

}

return 0;

}

POJ 2115 C Looooops【数论】的更多相关文章

  1. POJ 2115 C Looooops(扩展欧几里得应用)

    题目地址:POJ 2115 水题. . 公式非常好推.最直接的公式就是a+n*c==b+m*2^k.然后能够变形为模线性方程的样子,就是 n*c+m*2^k==b-a.即求n*c==(b-a)mod( ...

  2. 【题解】POJ 2115 C Looooops (Exgcd)

    POJ 2115:http://poj.org/problem?id=2115 思路 设循环T次 则要满足A≡(B+CT)(mod 2k) 可得 A=B+CT+m*2k 移项得C*T+2k*m=B-A ...

  3. POJ 2115 C Looooops(模线性方程)

    http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...

  4. POJ 2115 C Looooops(Exgcd)

    [题目链接] http://poj.org/problem?id=2115 [题目大意] 求for (variable = A; variable != B; variable += C)的循环次数, ...

  5. poj 2115 C Looooops——exgcd模板

    题目:http://poj.org/problem?id=2115 exgcd裸题.注意最后各种%b.注意打出正确的exgcd板子.就是别忘了/=g. #include<iostream> ...

  6. POJ 2115 C Looooops

    扩展GCD...一定要(1L<<k),不然k=31是会出错的 ....                        C Looooops Time Limit: 1000MS   Mem ...

  7. Poj 2115 C Looooops(exgcd变式)

    C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22704 Accepted: 6251 Descripti ...

  8. poj 2115 C Looooops 扩展欧几里德

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23616   Accepted: 6517 Descr ...

  9. POJ 2115 C Looooops扩展欧几里得

    题意不难理解,看了后就能得出下列式子: (A+C*x-B)mod(2^k)=0 即(C*x)mod(2^k)=(B-A)mod(2^k) 利用模线性方程(线性同余方程)即可求解 模板直达车 #incl ...

随机推荐

  1. 动手实现 React-redux(三):connect 和 mapStateToProps

    我们来观察一下刚写下的这几个组件,可以轻易地发现它们有两个重大的问题: 有大量重复的逻辑:它们基本的逻辑都是,取出 context,取出里面的 store,然后用里面的状态设置自己的状态,这些代码逻辑 ...

  2. sass+compass起步

    前言:Sass is an extension of CSS that adds power and elegance to the basic language. It allows you to ...

  3. 【转】10种简单的Java性能优化

    10种简单的Java性能优化 2015/06/23 | 分类: 基础技术 | 14 条评论 | 标签: 性能优化 分享到: 本文由 ImportNew - 一直在路上 翻译自 jaxenter.欢迎加 ...

  4. python中一些函数应用

    items将一个字典以列表的形式返回,因为字典是无序的,所以返回的列表也是无序的. 例如:a = {"a":1,"b":2}    a.items  就是 a ...

  5. 类成员的指针必须NULL化,否则是乱七八糟的东西

    class BiTree { public: BiTree(); virtual ~BiTree(); virtual void insertNode(Node * newNode); virtual ...

  6. 线程池ThreadPoolExecutor参数分析

    概述 比如去火车站买票, 有7个(maximumPoolSize)售票窗口, 但只有3个(corePoolSize)窗口对外开放.那么对外开放的3个窗口称为核心线程数, 而最大线程数是7个窗口. 如果 ...

  7. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'needDao' defined in URL

    这个是我修改过后的mapper,是我的mapper中的空间地址写错了呢

  8. oracle插入中文乱码

    执行sql: select  userenv('language')     from dual;  --  AMERICAN_AMERICA.ZHS16GBK select * from v$nls ...

  9. 【整理】iview中刷新页面的时候更新导航菜单的active-name

    iview中刷新页面的时候更新导航菜单的active-name https://blog.csdn.net/lhjuejiang/article/details/83212070

  10. 部署 k8s Cluster(上)[转]

    我们将部署三个节点的 Kubernetes Cluster. k8s-master 是 Master,k8s-node1 和 k8s-node2 是 Node. 所有节点的操作系统均为 Ubuntu ...