**链接:****传送门 **

题意:题目中给出一个循环 for (variable = A; variable != B; variable += C) ,这个东东还需要 mod 2^k 问至少多次能退出,如果进入死循环输出输出"FOREVER"

思路:简单拓欧嘛,简单分析一下 A + C * x = B + 2^k * y,如果方程有解,那么最小整数解就是最少次数,否则就是死循环,写了一下快速幂,不清楚普通求 2^k 也并不会T,还是快一点好


/*************************************************************************
> File Name: poj2115.cpp
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年05月21日 星期日 20时06分05秒
************************************************************************/ #include<bits/stdc++.h>
using namespace std; #define ll long long
ll exgcd(ll a,ll b,ll& x,ll& y){
if( b == 0 ){
x = 1; y = 0; return a;
}
ll d = exgcd( b , a % b , x , y );
ll tmp = x;
x = y; y = tmp - a/b*y;
return d;
}
ll quick_mod(ll a,ll x){
ll ret = 1;
while(x){
if( x & 1 ) ret = ret * a;
a = a * a;
x >>= 1;
}
return ret;
}
int main(){
ll a , b , c , k , x , y;
while(~scanf("%lld%lld%lld%lld",&a,&b,&c,&k)){
if( a == 0 && b == 0 && c == 0 && k == 0 ) break;
ll t = quick_mod(2,k);
ll d = exgcd( c , t , x , y );
ll c = b - a;
if( c % d != 0 ) printf("FOREVER\n");
else{
x = x*(c/d);
x = (x%(t/d) + (t/d))%(t/d);
printf("%lld\n",x);
}
}
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 3233 Matrix Power Series (矩阵快速幂)

    题目链接 Description Given a n × n matrix A and a positive integer k, find the sum S = A + A^2 + A^3 + - ...

  4. hdu4686 简单的矩阵快速幂求前n项和

    HDU4686 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意:题目说的很清楚了,英语不好的猜也该猜懂了,就是求一个表达式的前n项和,矩阵 ...

  5. POJ 3233:Matrix Power Series 矩阵快速幂 乘积

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 18450   Accepted:  ...

  6. 【POJ 1845】Sumdiv——数论 质因数 + 分治 + 快速幂

    (题面来自luogu) 题目描述 输入两个正整数a和b,求a^b的所有因子之和.结果太大,只要输出它对9901的余数. 输入格式 仅一行,为两个正整数a和b(0≤a,b≤50000000). 输出格式 ...

  7. POJ 3233 Matrix Power Series (矩阵快速幂+二分求解)

    题意:求S=(A+A^2+A^3+...+A^k)%m的和 方法一:二分求解S=A+A^2+...+A^k若k为奇数:S=(A+A^2+...+A^(k/2))+A^(k/2)*(A+A^2+...+ ...

  8. poj 3735 Training little cats(矩阵快速幂,模版更权威,这题数据很坑)

    题目 矩阵快速幂,这里的模版就是计算A^n的,A为矩阵. 之前的矩阵快速幂貌似还是个更通用一些. 下面的题目解释来自 我只想做一个努力的人 @@@请注意 ,单位矩阵最初构造 行和列都要是(猫咪数+1) ...

  9. POJ --- 3613 (K步最短路+矩阵快速幂+floyd)

    Cow Relays   Description For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided ...

随机推荐

  1. Sigma Function 数学 因子求和

    Sigma function is an interesting function in Number Theory. It is denoted by the Greek letter Sigma ...

  2. C#--线程池与线程的种类

    .NET CLR线程的种类 至少3个线程:主线程.调试线程.终结器线程 1.main thread 2.worker thread 3.finalizer thread 4.debugger help ...

  3. HDU 1115

    题意很明白要求多边形重心.方法已在上篇讲过了. #include <iostream> #include <cstdio> #include <cstring> # ...

  4. AutoReplace in pl/sql developer

    AutoReplace in pl/sql developer SL=SELECT S*=SELECT * FROM 2D=TO_DATE('2017-01-01 01:01:00','YYYY-MM ...

  5. 关于template 的23个问题

    发现新大陆.曾经慢慢才知道的东西.原来有个集中营: 看看updated, 处理方式是这么的好 35.1 " id="link-to-faq-35_1" style=&qu ...

  6. [LeetCode]Wildcard Matching 通配符匹配(贪心)

    一開始採用递归写.TLE. class Solution { public: bool flag; int n,m; void dfs(int id0,const char *s,int id1,co ...

  7. u-boot学习(五):u-boot启动内核

    u-boot的目的是启动内核.内核位于Flash中,那么u-boot就要将内核转移到内存中.然后执行命令执行之.这些操作是由bootcmd命令完毕的. bootcmd=nand read.jffs2 ...

  8. Java中static作用及使用方法具体解释

    1.1概述: static是静态修饰符,什么叫静态修饰符呢?大家都知道,在程序中不论什么变量或者代码都是在编译时由系统自己主动分配内存来存储的.而所谓静态就是指在编译后所分配的内存会一直存在.直到程序 ...

  9. jQuery右键菜单contextMenu实例

    URL: http://www.cnblogs.com/whitewolf/archive/2011/09/28/2194795.html http://www.blogjava.net/superc ...

  10. firewalld使用

    1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...