Codeforces Round #451 (Div. 2) A-B
来源:互联网 发布:外国人的审美 知乎 编辑:程序博客网 时间:2024/05/17 06:26
A:
int main(){ int n; cin>>n; if(n==0)cout<<"0"<<endl; else{ vector<int>Q; Q.clear(); int x=n; int sum=0; while(x) { Q.push_back(x%10); x/=10; } int len=Q.size(); if(Q[len-1]==0) cout<<x<<endl; else { int z=10-Q[0]; if(z>Q[0]) { printf("%d\n",n-Q[0]); } else printf("%d\n",n+z); }} return 0;}
123
B:
Vasya has n burles. One bottle of Ber-Cola costs a burles and one Bars bar costs b burles. He can buy any non-negative integer number of bottles of Ber-Cola and any non-negative integer number of Bars bars.
Find out if it's possible to buy some amount of bottles of Ber-Cola and Bars bars and spend exactly n burles.
In other words, you should find two non-negative integers x and y such that Vasya can buy x bottles of Ber-Cola and y Bars bars and x·a + y·b = n or tell that it's impossible.
First line contains single integer n (1 ≤ n ≤ 10 000 000) — amount of money, that Vasya has.
Second line contains single integer a (1 ≤ a ≤ 10 000 000) — cost of one bottle of Ber-Cola.
Third line contains single integer b (1 ≤ b ≤ 10 000 000) — cost of one Bars bar.
If Vasya can't buy Bars and Ber-Cola in such a way to spend exactly n burles print «NO» (without quotes).
Otherwise in first line print «YES» (without quotes). In second line print two non-negative integers x and y — number of bottles of Ber-Cola and number of Bars bars Vasya should buy in order to spend exactly n burles, i.e. x·a + y·b = n. If there are multiple answers print any of them.
Any of numbers x and y can be equal 0.
723
YES2 1
1002510
YES0 10
1548
NO
996059425512557
YES1951 1949
In first example Vasya can buy two bottles of Ber-Cola and one Bars bar. He will spend exactly 2·2 + 1·3 = 7 burles.
In second example Vasya can spend exactly n burles multiple ways:
- buy two bottles of Ber-Cola and five Bars bars;
- buy four bottles of Ber-Cola and don't buy Bars bars;
- don't buy Ber-Cola and buy 10 Bars bars.
In third example it's impossible to but Ber-Cola and Bars bars in order to spend exactly n burles.
扩展欧几里得: 求ax+by=z;
注意要非负数解
#include<iostream> #include<cstring> #include<cstdio> #include<malloc.h> using namespace std; typedef long long ll; inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll ans=exgcd(b,a%b,x,y);ll temp=x;x=y;y=temp-a/b*y;return ans;} int main() { ll a,b,x,y,c; cin>>c>>a>>b; ll eg=exgcd(a,b,x,y); if(c%eg) cout<<"NO"<<endl; else { x=x*c/eg; x=(x%(b/eg)+b/eg)%(b/eg); y=(c-x*a)/b; if(x<0|y<0) cout<<"NO"<<endl; else { cout<<"YES"<<endl; cout<<x<<" "<<y<<endl; } } return 0; }
- Codeforces Round #451 (Div. 2) A-B
- Codeforces Round #131 (Div. 2) A B
- Codeforces Round #185 (Div. 2)--A,B
- Codeforces Round #216 (Div. 2) A.B
- Codeforces round #236Div.2--A;B
- Codeforces Round #236 (Div. 2)------A,B
- Codeforces Round #237 (Div. 2) A & B
- Codeforces Round #247 (Div. 2) A B
- Codeforces Round #251 (Div. 2) A~B
- Codeforces Round #249 (Div. 2) A B
- Codeforces Round #271 (Div. 2)-A.B
- Codeforces Round #202 (Div. 2) A,B
- Codeforces Round #277 (Div. 2) A B
- Codeforces Round #278 (Div. 2) A B
- Codeforces Round #278 (Div. 2) A, B
- Codeforces Round #281 (Div. 2) A B
- Codeforces Round #283 (Div. 2) A B
- Codeforces Round #282 (Div. 2) A B
- 【Python】主(main)函数的原理
- n个数的排序
- PHP 类与对象(重载)
- Oracle Incident Response and Forensics.pdf 2017 英文原版 免费下载
- 二叉树的基本操作
- Codeforces Round #451 (Div. 2) A-B
- C语言(^)位异或运算符运算符 交换两个数位置
- 字母图形练习
- jzoj1935 [2011集训队出题] 单选错位 概率水题
- Linux系统的安装
- shiro的session和servlet的session的区别
- JDBC报错:Cannot find class: com.mysql.jdbc.Driver
- 周总结(一)
- 独家 | 国内首个 AI 题库上线