C Looooops POJ - 2115 (exgcd)
for(int i=A;i!=B;i+=C)
内容;
其中所有数都是k位二进制数,即所有数时膜2^k意义下的。我们的目标时球出 内容 被执行了多少次。
Input
Output
Sample Input
1 3 2 4
1 5 2 4
1 2 4 3
0 0 0 0
Sample Output
1
2
FOREVER 这和青蛙的那个题是一样的 想象成跑圈
写出式子 就可以了
注意最后对x的处理
ax + by = c
最后 b /= d;
x = (x % b + b) % b;
输出x即可
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff; LL gcd(LL a, LL b)
{
return b == ? a : gcd(b, a % b);
} LL exgcd(LL a, LL b, LL& d, LL& x, LL& y)
{
if(!b)
{
d = a;
x = ;
y = ;
}
else
{
exgcd(b, a % b, d, y, x);
y -= x * (a / b);
}
} int main()
{
LL a, b, c, k, x, y, d;
while(cin >> a >> b >> c >> k)
{
if(a == && b == && c == && k == )
break;
LL s = 1LL << k;
if((b - a) % gcd(c, s))
{
cout << "FOREVER" << endl;
continue;
}
exgcd(c, s, d, x, y);
s /= d;
x *= (b - a) / d;
x = (x % s + s) % s;
cout << x << endl;
} return ;
}
C Looooops POJ - 2115 (exgcd)的更多相关文章
- B - C Looooops POJ - 2115 (扩展欧几里得)
题目链接:https://cn.vjudge.net/contest/276376#problem/B 题目大意:for( int i= A ; i != B; i+ = c ),然后给你A,B,C ...
- 青蛙的约会 POJ - 1061 (exgcd)
两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特 ...
- 扩展欧几里得算法(EXGCD)学习笔记
0.前言 相信大家对于欧几里得算法都已经很熟悉了.再学习数论的过程中,我们会用到扩展欧几里得算法(exgcd),大家一定也了解过.这是本蒟蒻在学习扩展欧几里得算法过程中的思考与探索过程. 1.Bézo ...
- 【题解】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 ...
- C Looooops POJ - 2115 拓展gcd 有一个定理待补()
补算法导论P564 MODULAR-LINEAR-EQUATION-SOLVER算法(P564)
- POJ题目(转)
http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法: (1)枚举. (poj1753,poj29 ...
- E - The Balance POJ - 2142 (欧几里德)
题意:有两种砝码m1, m2和一个物体G,m1的个数x1, m2的个数为x2, 问令x1+x2最小,并且将天平保持平衡 !输出 x1 和 x2 题解:这是欧几里德拓展的一个应用,欧几里德求不定方程 ...
- 欧几里得(辗转相除gcd)、扩欧(exgcd)、中国剩余定理(crt)、扩展中国剩余定理(excrt)简要介绍
1.欧几里得算法(辗转相除法) 直接上gcd和lcm代码. int gcd(int x,int y){ ?x:gcd(y,x%y); } int lcm(int x,int y){ return x* ...
- Repeater POJ - 3768 (分形)
Repeater POJ - 3768 Harmony is indispensible in our daily life and no one can live without it----may ...
随机推荐
- [LeetCode] Rank Scores -- 数据库知识(mysql)
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
- 分解质因数FZU - 1075
题目简述:就是给一个数,把他拆分成多个素数的乘积,这正好是算术基本定理.本题我的解决方法是埃氏素数筛+质因数保存...开始T掉了,是因为我在最后枚举了素数,保存他们的次数,然后两次for去查询他们的次 ...
- Sampling Matrix
这些天看了一些关于采样矩阵(大概是这么翻译的)的论文,简单做个总结. FAST MONTE CARLO ALGORITHMS FOR MATRICES I: APPROXIMATING MATRIX ...
- Survey项目总结
1.Ioc深入理解 Inverse of control org.springframework.scheduling.quartz.SchedulerFactoryBean org.mybatis. ...
- 百度地图开发者API学习笔记二
一,地图上多个覆盖物(Marker). 当有多个覆盖物时,我们需要获取每个点的信息.如下图,每个Marker的经度都不相同 二,代码: <!DOCTYPE html> <html&g ...
- ansible jenkins war
Ansible is Simple IT Automationhttps://www.ansible.com/ Ansible中文权威指南- 国内最专业的Ansible中文官方学习手册http://a ...
- Git Gerrit Code Review
Gerrit Code Review | Gerrit Code Reviewhttps://www.gerritcodereview.com/
- CentOS下配置SS5(SOCKS5)代理服务器
方案:使用开源的SS5( Socks Server 5 ) 官网:http://ss5.sourceforge.net/ (点击左侧的Software在右侧的Server处进入下载地址) CentOs ...
- axios的增删改查。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 前端知识点总结(HTML)
前端知识点总结(HTML) 一,头部常用的标签 1,link标签 (1),设置ico图标 <link rel="shortcut icon" href="favi ...