当前位置: 首页 > news >正文

B - GCD Subtraction

文章目录

  • AtCoder Regular Contest 159
    • B - GCD Subtraction

AtCoder Regular Contest 159

B - GCD Subtraction

  1. 问题:每次A,B都减去gcd(A,B),求其中一个减到0至少需要多少次
  2. 主要思路:
    1. 首先第一步应该想到每次减去的数,先减去的数一定是后减去的数的因子,可以直接将A/gcd(A,B),B/gcd(A,B),计算两个互质数的答案
    2. gcd(A,B)=1,考虑什么时候不再减去1,假设为d,那么有 d|(A-t),d|(B-t),于是有 A = i ∗ d + t A = i*d+t A=id+t, B = j ∗ d + t B = j*d+t B=jd+t 1 ≤ t < d 1\le t <d 1t<d, d有以下性质
      d 是质数且 d ∣ ( A − B ) d 是质数 且 d| (A-B) d是质数且d(AB)
    3. 每次求 A − B A-B AB的所有质因子
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL gcd(LL a,LL b){return b ==0?a:gcd(b,a%b);
} 
void get(LL a,LL b,LL &ans) {if(a == 0 ||b == 0) return ;if(a > b) swap(a,b);LL _min = a;LL d = a;LL t = abs(a-b);LL tmp = t;vector<LL> prime;for(LL i = 2;i * i <= tmp; ++i) {if(t %i == 0) {prime.push_back(i);while(t%i==0) t/= i;}}if(t > 1) prime.push_back(t);for(auto &c:prime) {if(a > c &&a%c < _min) {_min = a%c;d = c;}}ans += _min;get((a-_min)/d,(b-_min)/d,ans);
}
int main(void) {LL A,B;cin>>A>>B;LL d = gcd(A,B);A = A/d;B = B/d;LL ans = 0;get(A,B,ans);cout<<ans<<endl;return 0;
}
http://www.lryc.cn/news/60071.html

相关文章:

  • 解决Failed to load ApplicationContext问题的思路
  • 基于CAMX大气臭氧来源解析模拟与臭氧成因分析实践技术应用
  • 异常的讲解 (1)
  • Prometheus - Grafana 监控 MySQLD Linux服务器 demo版
  • 应届生,实力已超6年,太卷了!
  • 0-1背包问题
  • VUE前端项目环境搭建
  • VMware安装Win2000安装程序闪退重启等问题的解决方法
  • 【id:45】【20分】A. Equation(类与对象+构造)
  • 数据库事务
  • Macbook(苹果电脑) VSCode 创建简单c++程序 配置C++开发环境
  • 如何使用 Matlab 构建深度学习模型
  • PDF怎么转CAD文件?(免费!高效转换方法汇总)
  • 经历了野蛮生长之后,新科技或许已经抵达了全新的临界点
  • Segment Anything论文翻译,SAM模型,SAM论文,SAM论文翻译;一个用于图像分割的新任务、模型和数据集;SA-1B数据集
  • EMQX vs NanoMQ | 2023 MQTT Broker 对比
  • RabbitMQ实现消息的延迟推送或延迟发送
  • 解决python中import导入自己的包呈现灰色 无效的问题
  • 消息中间件对比
  • nodejs+vue 高校校园食堂餐品在线订购网
  • SpringBoot【运维实用篇】---- SpringBoot程序的打包与运行
  • 10万字智慧政务数据中心平台建设方案
  • 使用 TensorFlow 构建机器学习项目:1~5
  • 【store商城项目08】删除用户的收获地址
  • SpringBooot
  • 测牛学堂:2023软件测试linux和shell脚本入门系列(shell的运算符)
  • TensorFlow 2.0 快速入门指南:第三部分
  • webpack介绍
  • SpringBoot 面试题汇总
  • 已知原根多项式和寄存器初始值时求LFSR的简单例子