POJ 2115 C Looooops(Exgcd)
【题目链接】 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)的更多相关文章
- 【题解】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 ...
- POJ - 2115 C Looooops(扩展欧几里德求解模线性方程(线性同余方程))
d.对于这个循环, for (variable = A; variable != B; variable += C) statement; 给出A,B,C,求在k位存储系统下的循环次数. 例如k=4时 ...
- poj 2115 C Looooops(推公式+扩展欧几里得模板)
Description A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; ...
- POJ 2115 C Looooops(模线性方程)
http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...
- POJ 2115 C Looooops( 简单拓欧 + 快速幂 )
链接:传送门 题意:题目中给出一个循环 for (variable = A; variable != B; variable += C) ,这个东东还需要 mod 2^k 问至少多次能退出,如果进入死 ...
- POJ 2142 The Balance(exgcd)
嗯... 题目链接:http://poj.org/problem?id=2142 AC代码: #include<cstdio> #include<iostream> using ...
- POJ 3669 Meteor Shower(流星雨)
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS Memory Limit: 65536K Description 题目描述 Bessie hears ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
随机推荐
- 关于 WizTools.org RESTClient的使用
今天分享一个很好用的测试service的工具,很好用 提供两种方法使用这个东东. 第一种方法 通过cmd命令窗口. (1)cd C:\Users\li_weifeng\Desktop\c4d2(文件存 ...
- JAX-WS 注解
一.概述 “基于 XML 的 Web Service 的 Java API”(JAX-WS)通过使用注释来指定与 Web Service 实现相关联的元数据以及简化 Web Service 的开发.注 ...
- Extend the size of ext3 partition online in VM
1. Increase disk space in vCenter 2. scan disk on the Linux VM # fdisk -lu > /tmp/fdisk. # > / ...
- 公共css
* { margin: 0; padding: 0; word-break: break-all; font-family: Microsoft YaHei, tahoma, arial, Hirag ...
- Java之戳中痛点 - (2)取余用偶判断,不要用奇判断
取余判断原则:取余用偶判断,不要用奇判断 先看一个 程序: package com.test; import java.util.Scanner; public class t1 { public s ...
- CSS3学习笔记之径向展开菜单
效果截图: HTML代码: <div class="menu-wrap"> <nav> <a href="" class=&quo ...
- 创建Maven项目出现:An internal error occurred during: "Retrieving archetypes:". Java heap space 错误解决办法
首先说明一下网上的方法: 在Eclipse中创建Maven的Web项目时出现错误:An internal error occurred during: "Retrieving archety ...
- nginx的常规配置
程序员们,在北上广你还能买房吗? >>> nginx的常规配置 nginx的使用非常简单,只需要配置好我们需要的各种指令,就能跑起来.如果你需要添加模块,还需要添加模块方面的配 ...
- hive向表格中插入数据并分析语句
1,---导入mds_imei_month_info ; //最大的动态分区表 set hive.support.concurrency=false; //是否支持并发 ; //each mapper ...
- RPC-Thrift(四)
Client Thrift客户端有两种:同步客户端和异步客户端. 同步客户端 同步客户端比较简单,以RPC-Thrift(一)中的的例子为基础进行研究源码,先看一下类图. TServiceClient ...