luogu 2152
SuperGcd
二进制算法
1. A = B, Gcd(A, B) = A;
2. A,B为偶数, Gcd(A, B) = 2 * Gcd(A / 2, B / 2);
3. A 为偶数, B 为奇数,Gcd(A, B) = Gcd(A / 2, B);
4. A,B为奇数, Gcd(A, B) = Gcd(A - B, B)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring> using namespace std;
const int N = 2e5 + ; int C[N * ];
char s[N]; struct Node {
inline void Init(int A[]) {
scanf("%s", s); int Len = strlen(s); A[] = Len; for(int i = ; i <= Len; i ++) A[i] = s[i - ] - '';
}
inline int Cmp(int A[], int B[]) {
if(A[] > B[]) return ; if(A[] < B[]) return -;
for(int i = ; i <= A[]; i ++) if(A[i] > B[i]) return ; else if(A[i] < B[i]) return -;
return ;
}
inline void Div(int A[]) {
int tmp(), j = ;
for(int i = ; i <= A[]; i ++) {tmp += A[i]; A[i] = tmp / ; tmp %= ; tmp *= ;}
for(int i = ; i <= A[] && !A[i]; i ++, j = i); A[] = A[] - j + ;
for(int i = ; i <= A[]; i ++) A[i] = A[i + j - ];
}
inline void Minus(int A[], int B[]) {
for(int i = B[], j = A[]; i >= ; i --, j --) {
if(A[j] < B[i]) {A[j] += ; A[j - ] --;} A[j] -= B[i];
}
int j = ;
for(int i = ; i <= A[] && !A[i]; i ++, j = i); A[] = A[] - j + ;
for(int i = ; i <= A[]; i ++) A[i] = A[i + j - ];
}
int a[N], b[N];
inline void Mul(int A[], int B[]) {
memset(C, , sizeof C);
int j = ; for(int i = A[]; i >= ; i --) a[++ j] = A[i];
j = ; for(int i = B[]; i >= ; i --) b[++ j] = B[i];
for(int i = ; i <= A[]; i ++) {
int x = ;
for(int k = ; k <= B[]; k ++) {C[i + k - ] += a[i] * b[k] + x; x = C[i + k - ] / ; C[i + k - ] %= ;}
C[i + B[]] = x;
}
j = A[] + B[]; while(!C[j] && j > ) j --; A[] = ;
for(int i = j; i >= ; i --) A[++ A[]] = C[i];
}
inline void Out(int A[]) {for(int i = ; i <= A[]; i ++) printf("%d", A[i]);}
} Big_num_work; void Gcd(int A[], int B[], int &tot) {
int imp = Big_num_work.Cmp(A, B); if(imp == ) return ; if(imp < ) {Gcd(B, A, tot); return ;}
int ta(), tb(); if(A[A[]] % == ) Big_num_work.Div(A), ta = ; if(B[B[]] % == ) Big_num_work.Div(B), tb = ;
if(ta && tb) Gcd(A, B, ++ tot);
else if(!ta && !tb) {Big_num_work.Minus(A, B); Gcd(A, B, tot);}
else Gcd(A, B, tot);
} int A[N], B[N], F[N]; int main() {
Big_num_work.Init(A); Big_num_work.Init(B);
int T = ; Gcd(A, B, T);
if(T == ) Big_num_work.Out(A);
else {
int Tow[]; Tow[] = , Tow[] = ; F[] = F[] = ; for(int i = ; i <= T; i ++) Big_num_work.Mul(F, Tow);
Big_num_work.Mul(A, F); Big_num_work.Out(A);
}
return ;
}
luogu 2152的更多相关文章
- BZOJ 2152 / Luogu P2634 [国家集训队]聪聪可可 (点分治/树形DP)
题意 一棵树,给定边权,求满足两点之间的路径上权值和为3的倍数的点对数量. 分析 点分治板题,对每个重心求子树下面的到根的距离模3分别为0,1,2的点的个数就行了. O(3nlogn)O(3nlogn ...
- BZOJ 2152 & 点分治
Description: 只是打法法塔前测试一下板子 Code: /*================================= # Created time: 2016-04-20 14:3 ...
- Luogu 魔法学院杯-第二弹(萌新的第一法blog)
虽然有点久远 还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题 沉迷游戏,伤感情 #include <queue> ...
- luogu p1268 树的重量——构造,真正考验编程能力
题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...
- BZOJ 2152: 聪聪可可 树分治
2152: 聪聪可可 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...
- Code[VS] 2152 滑雪题解
Code[VS] 2152 滑雪题解 题目描述 Description trs喜欢滑雪.他来到了一个滑雪场,这个滑雪场是一个矩形,为了简便,我们用r行c列的矩阵来表示每块地形.为了得到更快的速度,滑行 ...
- bzoj 2152聪聪可可
2152: 聪聪可可 Time Limit: 3 Sec Memory Limit: 259 MB Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰 ...
- 【BZOJ】2152: 聪聪可可(点分治)
http://www.lydsy.com/JudgeOnline/problem.php?id=2152 随便点分..... 只是我在考虑一个地方逗乐.. 当路径长度mod3=0的点数直接乘起来就好. ...
- sdutoj 2152 Balloons
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2152 Balloons Time Limit: ...
随机推荐
- 【Linux】一步一步学Linux——Linux版本(03)
目录 00. 目录 01. Linux内核版本 02. Linux内核官方网站 03. Linux发行版本 04. Linux发行版本介绍 4.1 Ubuntu 4.2 RedHat 4.3 Debi ...
- C#在DataTable中使用LINQ
LINQ 查询适用于实现的数据源 IEnumerable<T>接口或System.Query.IQueryable接口. DataTable类默认是没有实现以上接口的. 所以要在DataT ...
- windowsAPI创建句柄失败的返回值
创建句柄的api返回值 INVALID_HANDLE_VALUE CreateFile CreateNamedPipe CreateToolhelp32Snapshot FilterConnectCo ...
- sqlserver-order by offset fetch
若要使用 OFFSET 和 FETCH 在查询请求之间获得稳定的结果,必须满足以下条件: 查询使用的基础数据不能发生变化. 即,不会更新查询处理的行,也不会在单个事务中使用快照或可序列化事务隔离执行查 ...
- 《图解HTTP》摘录
# 图解HTTP 第 1 章 了解Web及网络基础 1.1使用http协议访问web 客户端:通过发送请求获取服务器资源的Web浏览器等. Web使用一种名为 HTTP(HyperText Trans ...
- LA服务可用性4个9是什么意思?怎么达到?
SLA:服务等级协议(简称:SLA,全称:service level agreement).是在一定开销下为保障服务的性能和可用性,服务提供商与用户间定义的一种双方认可的协定.通常这个开销是驱动提供服 ...
- BASIS小问题汇总1
try to start SAP system but failed 2019-04-04 Symptom: when i tried to start SAP system, using the c ...
- Jerry带您了解Restful ABAP Programming模型系列之二:Action和Validation的实现
相信通过Jerry的前一篇文章 30分钟用Restful ABAP Programming模型开发一个支持增删改查的Fiori应用,想必大家对Restful ABAP Programming模型已经有 ...
- KVM虚拟机快照链创建,合并,删除及回滚研究
1 QEMU,KVM,libvirt关系 QEMU QEMU提供了一个开源的服务器全虚拟化解决方案,它可以使你在特定平台的物理机上模拟出其它平台的处理器,比如在X86 CPU上虚拟出Power的CPU ...
- 基于beautifulSoup进行电影网站排名的获取与格式化输出
要求 编写代码完成以下任务: ① 将地址"http://www.cbooo.cn/year?year=2019"源代码使用任意方法保存到指定文件中(文件类型不限). ② 使用文件流 ...