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 ...
随机推荐
- ORM 多表操作查询及增删改查
------------------------------------------只有对前途乐观的人,才能不怕黑暗,才能有力量去创造光明.乐观不是目的,而是人生旅途中的一种态度. 多表操作 创建模型 ...
- 295B - Greg and Graph (floyd逆序处理)
题意:给出任意两点之间的距离,然后逐个删除这些点和与点相连的边,问,在每次删除前的所有点对的最短距离之和 分析:首先想到的是floyd,但是如果从前往后处理,复杂度是(500)^4,超时,我们从后往前 ...
- c++入门之再话内存和引用
此处没有代码,仅仅讨论一些这样的问题:我们为何使用引用?在哪里使用引用? 首先从函数的角度思考?:函数进行一般参数传递的时候,是怎么样传递的?普通类型的参数传递,是将传递的实参复制一份,到另一个内存空 ...
- ImportError: DLL load failed: 找不到指定的模块。
这里用的anacoda,报错是找不到DLL,可能是该DLL的环境变量没配置,配置系统环境变量: 重启一下pycharm,OK.
- 各种jar包
springframework下载地址:http://maven.springframework.org/release/org/springframework/spring/ commons开头的j ...
- 爬虫——xpath
1.什么是xpath? Xpath,全称XML Path Language,即XML路径语言.它是一门在XML之后查找信息的语言,也同样适用于HTML文档的搜索.在做爬虫的时候,我们用XPath语言来 ...
- Eclipse lombok java
Stablehttps://projectlombok.org/features/all Lombok介绍及使用方法 - holten - 博客园http://www.cnblogs.com/holt ...
- oracle事务的四个特性(ACID)
事务产生的背景 当在PL/SQL中同时操作多个SQL语句,比如通过DML语句添加.修改或删除数据时,如何确保数据库数据不会因为意外而倒置错误数据是一个非常重要的问题. 以仓库发料系统为例,如果某一张领 ...
- [转帖]前端-chromeF12 谷歌开发者工具详解 Console篇
前端-chromeF12 谷歌开发者工具详解 Console篇 https://blog.csdn.net/qq_39892932/article/details/82655866 趁着搞 cloud ...
- CSS3 background-size属性兼容
background-size是CSS3新增的属性,但是IE8以下还是不支持 background-size:contain; // 缩小图片来适应元素的尺寸(保持像素的长宽比):background ...