【题目链接】 http://poj.org/problem?id=2115

【题目大意】

  求for (variable = A; variable != B; variable += C)的循环次数,
  其中变量为k比特无符号整数。

【题解】

  题目等价于求解Cx=(B–A)(mod 2^k),利用扩展欧几里得算法可以求解该问题

【代码】

#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
ll exgcd(ll a,ll b,ll&x,ll&y){
if(!b)return x=1,y=0,a;
ll d=exgcd(b,a%b,x,y),t=x;
return x=y,y=t-a/b*y,d;
}
ll A,B,C,k;
int main(){
while(scanf("%lld%lld%lld%lld",&A,&B,&C,&k),A+B+C+k){
ll a=C,b=B-A,n=1LL<<k,x,y,d=exgcd(a,n,x,y);
if(b%d)puts("FOREVER");
else{
x=(x*(b/d))%n; //方程ax=b(mod n)的最小解
x=(x%(n/d)+n/d)%(n/d); //方程ax=b(mod n)的最小整数解
printf("%I64d\n",x);
}
}return 0;
}

POJ 2115 C Looooops(Exgcd)的更多相关文章

  1. 【题解】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 ...

  2. POJ - 2115 C Looooops(扩展欧几里德求解模线性方程(线性同余方程))

    d.对于这个循环, for (variable = A; variable != B; variable += C) statement; 给出A,B,C,求在k位存储系统下的循环次数. 例如k=4时 ...

  3. poj 2115 C Looooops(推公式+扩展欧几里得模板)

    Description A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; ...

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

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

  5. POJ 2115 C Looooops( 简单拓欧 + 快速幂 )

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

  6. POJ 2142 The Balance(exgcd)

    嗯... 题目链接:http://poj.org/problem?id=2142 AC代码: #include<cstdio> #include<iostream> using ...

  7. POJ 3669 Meteor Shower(流星雨)

    POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS    Memory Limit: 65536K Description 题目描述 Bessie hears ...

  8. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

  9. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

随机推荐

  1. CentOs7 minimal安装后没有ifconfig命令解决方法

    没有ifconfig命令目前我了解两个原因: 1./sbin/ifconfig 可以执行,但是ifconfig无法执行.这个解决的时候只需要将/sbin 添加到PATH下就可以了. 2.系统未安装if ...

  2. 牛客多校对抗第6场 A Singing Contest

    [20分]标题:A.Singing Contest | 时间限制:1秒 | 内存限制:256MJigglypuff is holding a singing contest. There are 2n ...

  3. PowerShell官方文档

    PowerShell PowerShell 在 .NET Framework 基础之上构建,是一种基于任务的命令行 Shell 脚本语言:专门面向系统管理员和高级用户,可快速自动化多个操作系统(Lin ...

  4. Join an instance to my AWS Directory Service domain

    https://amazonaws-china.com/cn/premiumsupport/knowledge-center/ec2-systems-manager-dx-domain/ https: ...

  5. C# 程序Hello World

    先创建一个工程文件->选择的是console application. 然后开始写代码如下: using System; using System.Collections.Generic; us ...

  6. 403 Forbidden 错误

    Forbidden You don't have permission to access /a.php on this server. apache昨天调试 httpd.conf 文件:<Di ...

  7. 优化IDEA启动速度,快了好多。后面有什么优化点,会继续往里面添加

    1.优化启动 修改bin/idea.exe.vmoptions文件如下: -Xms256m   初始堆大小-Xmx384m   最大堆大小 -XX:+UseParNewGC   使用并行收集算法 2. ...

  8. 【NOIP1999】邮票面值设计 dfs+dp

    题目传送门 这道题其实就是找一波上界比较麻烦 用一波 背包可以推出上界mx 所以新加入的物品价值一旦大于mx+1,显然就会出现断层,所以可以以maxm+1为枚举上界,然后这样进行下一层的dfs. 这样 ...

  9. date "+Y-%m-%d %H:%M"

     date "+Y-%m-%d %H:%M"    date | awk '{print "Year:"$6  "\t month:"$2  ...

  10. Python selenium.webdriver.chrome.options.Options() Examples

    The following are 27 code examples for showing how to use selenium.webdriver.chrome.options.Options( ...