POJ2115:C Looooops——题解
http://poj.org/problem?id=2115
题目大意:for(i=A;i!=B;i+=C),i的类型的范围为0<=a<1<<k
exgcd裸题目。
设a=C,b=(1<<k),c=(B-A).
则ax+by=c.
#include<cstdio>
#include<cctype>
#include<iostream>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
void exgcd(ll a,ll b,ll &x,ll &y){
if(b==){
x=;y=;
return;
}
exgcd(b,a%b,x,y);
ll temp;
temp=x;
x=y;
y=temp-(a/b)*y;
return;
}
int main(){
ll A,B,C,k;
while(cin>>A>>B>>C>>k){
if(A==B&&B==C&&C==k&&A==)return ;
ll x,y;
ll a=C,b=(ll)<<k,c=B-A;
if(!c){
printf("0\n");
continue;
}
ll g=gcd(a,b);
if(c%g){
printf("FOREVER\n");
continue;
}
a/=g;b/=g;c/=g;
exgcd(a,b,x,y);
x=(x%b*c%b+b)%b;
printf("%lld\n",x);
}
return ;
}
#include<cstdio>
#include<cctype>
#include<iostream>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
void exgcd(ll a,ll b,ll &x,ll &y){
if(b==0){
x=1;y=0;
return;
}
exgcd(b,a%b,x,y);
ll temp;
temp=x;
x=y;
y=temp-(a/b)*y;
return;
}
int main(){
ll A,B,C,k;
while(cin>>A>>B>>C>>k){
if(A==B&&B==C&&C==k&&A==0)return 0;
ll x,y;
ll a=C,b=(ll)1<<k,c=B-A;
if(!c){
printf("0\n");
continue;
}
ll g=gcd(a,b);
if(c%g){
printf("FOREVER\n");
continue;
}
a/=g;b/=g;c/=g;
exgcd(a,b,x,y);
x=(x%b*c%b+b)%b;
printf("%lld\n",x);
}
return 0;
}
POJ2115:C Looooops——题解的更多相关文章
- poj2115 C Looooops(exgcd)
poj2115 C Looooops 题意: 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束. 若在有限次内结束,则输出循环次数. 否则输出死循环. ...
- POJ2115——C Looooops(扩展欧几里德+求解模线性方程)
C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...
- POJ2115 C Looooops 扩展欧几里德
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2115 题意 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次 ...
- POJ2115 C Looooops ——模线性方程(扩展gcd)
题目链接:http://poj.org/problem?id=2115 C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ2115 C Looooops[扩展欧几里得]
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24355 Accepted: 6788 Descr ...
- POJ2115 - C Looooops(扩展欧几里得)
题目大意 求同余方程Cx≡B-A(2^k)的最小正整数解 题解 可以转化为Cx-(2^k)y=B-A,然后用扩展欧几里得解出即可... 代码: #include <iostream> us ...
- poj2115 C Looooops
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29262 Accepted: 8441 Descr ...
- POJ2115 C Looooops(线性同余方程)
无符号k位数溢出就相当于mod 2k,然后设循环x次A等于B,就可以列出方程: $$ Cx+A \equiv B \pmod {2^k} $$ $$ Cx \equiv B-A \pmod {2^k} ...
- POJ2115 C Looooops 模线性方程(扩展欧几里得)
题意:很明显,我就不说了 分析:令n=2^k,因为A,B,C<n,所以取模以后不会变化,所以就是求(A+x*C)%n=B 转化一下就是求 C*x=B-A(%n),最小的x 令a=C,b=B-A ...
随机推荐
- HTML5 离线应用程序
离线Web应用:当客户端本地与Web应用程序的服务器没有建立连接时,也能正常在客户端本地使用该Web应用. Web应用程序的本地缓存与浏览器的网页缓存的区别 1. 本地缓存为整个Web应用程序服务,网 ...
- iOS 测试工具reveal可视化调试工具的使用
简单翻译一下reveal可视化图形工具插入项目的官方文档(官方英文版file:///Applications/Reveal.app/Contents/SharedSupport/Documentati ...
- apache Subversion 直接支持LDAP域群组
如果你的Subversion已经用apache的ldap支持用户认证功能,你是否常常在想,既然都用ldap支持认证,为什么不直接支持域群组, 反而在authz文件里面一个一个的手工定义,或者有人用脚本 ...
- 「功能笔记」性能分析工具gprof使用笔记
根据网上信息整理所成. 功能与优劣 gprof实际上只是一个用于读取profile结果文件的工具.gprof采用混合方法来收集程序的统计信息,它使用检测方法,在编译过程中在函数入口处插入计数器用于收集 ...
- Ping隧道
1.研究原因: 校园内网的探索,校内电子图书馆资源的利用,认证校园网 2.目的: 内网服务器:在一台因防火墙等原因仅限icmp数据通过的 公网服务器 : 建立icmp 隧道链接, 并在公网服务器上进 ...
- 小程序button 去边框
/*使用 button::after{ border: none; } 来去除边框*/.free-btn-bordernone{ background: none !important; color: ...
- 407. Plus One【LintCode java】
Description Given a non-negative number represented as an array of digits, plus one to the number. T ...
- docker最佳实践-----美团点评的分享
美团点评容器平台简介 本文介绍美团点评的Docker容器集群管理平台(以下简称“容器平台”).该平台始于2015年,是基于美团云的基础架构和组件而开发的Docker容器集群管理平台.目前该平台为美团点 ...
- POJ 1755 Triathlon(线性规划の半平面交)
Description Triathlon is an athletic contest consisting of three consecutive sections that should be ...
- VUE中组件的使用
关于vue组件引用 使用Nodejs的方法 被引用的组件要暴露 module.exports={}; 引用时 用 var abc= require("组件的路径") 然后 就可以用 ...