数学思维——cf1244C
可惜cf不能用int128,不然这个题就是个exgcd的板子题
这是exgcd的解法,但是只用ll的话会溢出
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll x,y,z,a,b,c,d,n; ll exgcd(ll a,ll b,ll &x,ll &y){//返回(a,b)
if(b==){x=,y=;return a;}
ll d=exgcd(b,a%b,x,y);
ll z=x;x=y,y=z-a/b*y;
return d;
} int main(){
cin>>n>>c>>a>>b;//1e12,c:1e17,a:1e5,b:1e5
d=__gcd(a,b);
if(c%d!=){
puts("-1");return ;
} exgcd(a,b,x,y);
//923399641127 50915825165227299 94713 49302
x*=c/d;y*=c/d;
ll mod1=b/d,mod2=a/d; //先让x,y都变成非负整数
if(x<){
ll k;
if(x%mod1==)k=abs(x/mod1);
else k=abs(x/mod1)+;
x+=k*mod1;
y-=k*mod2;
}
else if(y<){
ll k;
if(y%mod2==)k=abs(y/mod2);
else k=abs(y/mod2)+;
x-=k*mod1;
y+=k*mod2;
} if(x< || y<){
puts("-1");return ;
}
if(x>n && y>n){
puts("-1");return ;
}
//让x+y的值取到最小值
if(mod1>mod2){
ll k=x/mod1;
x-=k*mod1;
y+=k*mod2;
}
else {
ll k=y/mod2;
x+=k*mod1;
y-=k*mod2;
}
if(x+y>n){puts("-1");return ;}
cout<<x<<" "<<y<<" "<<n-x-y<<"\n";
}
这是直接枚举的办法
/*
ax+by=c;
x+y+z=n;
b<a<=1e5; c<=1e17; n<=1e12
性质:如果有解,那么一定有y<a的一个解
假设有解 (x,y=y'+a)
ax+b(y'+a)=c
ax+by'+ba=c;
a(x+b)+by'=c,即必定有一个解是(x+b,y')
所以枚举y=[0,a-1]即可
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,a,b,c;
int main(){
cin>>n>>c>>a>>b;
for(ll y=;y<a;y++){
ll t=c-b*y;
if((t>=) && t%a==){
ll x=t/a;
if(x+y<=n){
cout<<x<<" "<<y<<" "<<n-x-y;
return ;
}
}
}
puts("-1");
}
数学思维——cf1244C的更多相关文章
- 程序设计中的数学思维函数总结(代码以C#为例)
最近以C#为例,学习了程序设计基础,其中涉及到一些数学思维,我们可以巧妙的将这些逻辑问题转换为代码,交给计算机运算. 现将经常会使用到的基础函数做一总结,供大家分享.自己备用. 1.判断一个数是否为奇 ...
- PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记
PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...
- UVa10025 The ? 1 ? 2 ? ... ? n = k problem 数学思维+规律
UVa10025 ? 1 ? 2 ? ... ? n = k problem The problem Given the following formula, one can set operator ...
- B. Tell Your World(几何数学 + 思维)
B. Tell Your World time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- hdu 4710 Balls Rearrangement (数学思维)
意甲冠军:那是, 从数0-n小球进入相应的i%a箱号.然后买一个新的盒子. 今天的总合伙人b一个盒子,Bob试图把球i%b箱号. 求复位的最小成本. 每次移动的花费为y - x ,即移动前后盒子编号 ...
- F. Multicolored Markers(数学思维)
思维:思维就是将大的矩形放在小矩形里面,让大矩形的宽和长尽量靠近. 很容易得到 (a+b)% i = 0 的话, 保证了大矩形的形成,同时里面表示了两种情况:1, a % i =0, b % i=0; ...
- Pythagorean Triples毕达哥斯拉三角(数学思维+构造)
Description Katya studies in a fifth grade. Recently her class studied right triangles and the Pytha ...
- HDU - 6409:没有兄弟的舞会(数学+思维)
链接:HDU - 6409:没有兄弟的舞会 题意: 题解: 求出最大的 l[i] 的最大值 L 和 r[i] 的最大值 R,那么 h 一定在 [L, R] 中.枚举每一个最大值,那么每一个区间的对于答 ...
- Wannafly交流赛1 B 硬币[数学思维/贪心]
链接:https://www.nowcoder.com/acm/contest/69/B来源:牛客网 蜥蜴的生日快到了,就在这个月底! 今年,蜥蜴的快乐伙伴之一壁虎想要送好多个1元硬币来恶整蜥蜴. 壁 ...
随机推荐
- 【leetcode】1003. Check If Word Is Valid After Substitutions
题目如下: We are given that the string "abc" is valid. From any valid string V, we may split V ...
- BlueStore-先进的用户态文件系统《二》-BlueFS
https://zhuanlan.zhihu.com/p/46362124 简介 上一篇文章中,介绍了BlueStore的诞生背景.逻辑架构以及设计思想,提到了在BlueStore中元数据都是存放在R ...
- sigaction函数学习
sigaction(查询或设置信号处理方式) 相关函数 signal,sigprocmask() ,sigpending,sigsuspend, sigemptyset 表头文件 #include&l ...
- SPOJ - VLATTICE (莫比乌斯反演)
Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at (N,N,N). How many latt ...
- 为什么不能在shell脚本中执行source /etc/profile或者source ~/.bashrc问题?
执行脚本时,其中的命令是在一个子shell中执行的.子shell继承了父shell的环境变量,但无法修改他们,或者说所做的修改仅对子shell有效.
- delphi弹出选择对话框选择目录SelectDirectory 函数
SelectDirectory 函数通过显示一个对话框来让用户选择一个目录.注意:在使用前要在Uses 语句中添加包含FileCtrl 的说明.函数原型如下:function SelectDirect ...
- CJE-Jenkins认证工程师备考指南1-考试简介
CloudBees公司提供两项认证 Jenkins工程师(CJE)考试 包括60个选择题 测试开源Jenkins的知识. CloudBees 平台工程师(CCJE)考试 包含90个问题: 60个问题测 ...
- HDU 1875 畅通工程再续 (Prim)
题目链接:HDU 1875 Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现 ...
- POJ 2001 Shortest Prefixes (Trie)
题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...
- upc组队赛6 Progressive Scramble【模拟】
Progressive Scramble 题目描述 You are a member of a naive spy agency. For secure communication,members o ...