51nod1352(exgcd)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1352
题意:中文题诶~
思路:exgcd
显然题目可以描述为:求a*x+b*y=n+1中满足 1 <= x,y <=n 的解数,
可以先通过exgcd求出一组a*x+b*y=gcd(a, b)的解 x1, y1,那么对应的a*x+b*y=n+1的解就是x1*(n/gcd(a, b)), y1*(n/(gcd(a, b)),
若能求出最小的x解的话,则每隔lcm(a, b), 隔lcm(a, b)出现一组满足条件的解,所以有ans=(n-1-x*a)/lcm(a,b) + 1;
先令x=x1%b,要尽量使x小,所以将大于b的部分放到b*y中去;
令temp=x*a, cc=lcm(a, b)
则有:
while(temp<1){
temp+=cc;
}
while(temp>0){
temp-=cc;
}
temp+=cc;// 第一个大于0的a*x
将其直接化为公式计算:
if(temp<1){
k=ceil(double(1-temp)/cc);
temp+=cc*k;
}else{
k=(temp-1)/cc;
temp-=cc*k;
}
答案也就显而易见了,注意中间可能会爆int....
代码:
#include <iostream>
#include <stdio.h>
#include <math.h>
#define ll long long
using namespace std; int exgcd(ll a, ll b, ll& d, ll& x, ll& y){
if(b==){
x=, y=, d=a;
}else{
exgcd(b, a%b, d, y, x);
y-=(a/b)*x;
}
} int main(void){
int t;
ll n, a, b;
scanf("%d", &t);
while(t--){
scanf("%lld%lld%lld", &n, &a, &b);
ll x, y, d;
exgcd(a, b, d, x, y);
if((++n)%d){ //a*x+b*y=c 当且仅当c=k*gcd(a,b)时有整数解
printf("0\n");
continue;
}
x=x*(n/d)%b; //得到a*x+b*y=n+1的解,若x>b,将大于b的部分放到y*b中
ll cc=a*b/d; //lcm(a,b)
ll temp=x*a;
// while(temp<1){
// temp+=cc;
// }
// while(temp>0){
// temp-=cc;
// }
// temp+=cc;// 第一个大于0的a*x
ll k;
if(temp<){
k=ceil(double(-temp)/cc);
temp+=cc*k;
}else{
k=(temp-)/cc;
temp-=cc*k;
}
if(temp>=n){
printf("0\n");
}else{
printf("%lld\n", (n-temp-)/cc+);//前面给n加了1,但求出的b*y要<=n
}
}
return ;
}
51nod1352(exgcd)的更多相关文章
- 扩展欧几里得 exGCD
Elementary Number Theory - Extended Euclid Algorithm Time Limit : 1 sec, Memory Limit : 65536 KB Jap ...
- NOIP2012同余方程[exgcd]
题目描述 求关于 x 的同余方程 ax ≡ 1 (mod b)的最小正整数解. 输入输出格式 输入格式: 输入只有一行,包含两个正整数 a, b,用一个空格隔开 输出格式: 输出只有一行,包含一个正整 ...
- exgcd,求乘法逆元
procedure exgcd(a,b:int64); var t:longint; begin then begin x:=;y:=; exit; end else exgcd(b,a mod b) ...
- 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数
1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(l ...
- 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)
4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 290 Solved: 148[Submit][Status ...
- poj1061 Exgcd
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> usin ...
- 51Nod 1256 乘法逆元 Label:exgcd
1256 乘法逆元 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K < N且K ...
- 【BZOJ2242】【SDoi2011】计算器 快速幂+EXGCD+BSGS
Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...
- Poj 2115 C Looooops(exgcd变式)
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22704 Accepted: 6251 Descripti ...
随机推荐
- MVC入门——编辑页
添加Action EditUserInfo using System; using System.Collections.Generic; using System.Linq; using Syst ...
- 在OC项目中实现swift与oc混编 相互引用
--------------------------------------------------------Begin--------------------------------------- ...
- GitLab Pages expect to run on their own virtual host
GitLab Pages administration | GitLab https://docs.gitlab.com/ce/administration/pages/
- Java类加载器( 死磕9)
[正文]Java类加载器( CLassLoader ) 死磕9: 上下文加载器原理和案例 本小节目录 9.1. 父加载器不能访问子加载器的类 9.2. 一个宠物工厂接口 9.3. 一个宠物工厂管理 ...
- db的操作
'/---------------------------------------------------------------------------------------------- '/ ...
- Tomcat Session Clustering
搭建 Tomcat 集群需要解决很多的问题,其中之一就是要解决 Session 共享问题.小规模集群可以使用 Tomcat 提供的 Session Clustering 来解决. For the im ...
- 需要注意的一些Mysql语句
1. 日期处理函数:date_format() select COUNT(*) from (SELECT SERIAL_NO, APPLY_SERIAL_NO, FLAG, PAY_DATE, SEQ ...
- UVA1025 A Spy in the Metro —— DP
题目链接: https://vjudge.net/problem/UVA-1025 题解: 详情请看紫书P267. 与其说是DP题,我觉得更像是模拟题,特别是用记忆化搜索写. 递推: #include ...
- 'gbk' codec can't encode character '\xa5' in position 4546: illegal multibyte sequence错误解决
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb18030') 原文 http://blog.csdn.net/jim7424 ...
- ModuleNotFoundError: No module named 'numpy.core._multiarray_umath' ImportError: numpy.core.multiarray failed to import
出现以下错误:可能是因为你的numpy版本太低 更新numpy的版本 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgra ...