Coupons UVA - 10288
2017-11-10 14:04
232 查看
推导出最终的式子,然后编程求解即可,注意最大公约数与最小公倍数的使用,具体实现见如下代码:
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
#include<functional>
using namespace std;
typedef long long LL;
LL gcd(LL a,LL b){
if (!b) return a;
return gcd(b, a%b);
}
LL lcm(LL a,LL b){
return a / gcd(a, b)*b;
}
LL length(LL data){
stringstream ss;
ss << data;
return ss.str().length();
}
void putChar(int amount,char c){
for (int i = 0; i < amount; i++)
cout << c;
}
void getRes(LL left,LL up,LL down){
if (up == 0){
cout << left << endl;
}
else{
int L1 = length(left);
putChar(L1+1,' ');
cout << up<<endl;
cout << left << " ";
int L = length(down);
putChar(L,'-');
cout << endl;
putChar(L1 + 1, ' ');
cout << down << endl;
}
}
int main(){
LL n;
while (cin >> n){
if (n == 1){
cout << "1\n";
}
else{
LL x=1;
for (LL i = 2; i <= n - 1; i++){
x = lcm(x,i);
}
LL up = 0,down=x;
for (LL i = 2; i <= n - 1; i++){
up += (x / i);
}
up = up*n;
LL c_ud = gcd(up,down);
up = up / c_ud, down = down / c_ud;
LL left = 1 + n + up / down;
getRes(left,up%down,down);
}
}
return 0;
}
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
#include<functional>
using namespace std;
typedef long long LL;
LL gcd(LL a,LL b){
if (!b) return a;
return gcd(b, a%b);
}
LL lcm(LL a,LL b){
return a / gcd(a, b)*b;
}
LL length(LL data){
stringstream ss;
ss << data;
return ss.str().length();
}
void putChar(int amount,char c){
for (int i = 0; i < amount; i++)
cout << c;
}
void getRes(LL left,LL up,LL down){
if (up == 0){
cout << left << endl;
}
else{
int L1 = length(left);
putChar(L1+1,' ');
cout << up<<endl;
cout << left << " ";
int L = length(down);
putChar(L,'-');
cout << endl;
putChar(L1 + 1, ' ');
cout << down << endl;
}
}
int main(){
LL n;
while (cin >> n){
if (n == 1){
cout << "1\n";
}
else{
LL x=1;
for (LL i = 2; i <= n - 1; i++){
x = lcm(x,i);
}
LL up = 0,down=x;
for (LL i = 2; i <= n - 1; i++){
up += (x / i);
}
up = up*n;
LL c_ud = gcd(up,down);
up = up / c_ud, down = down / c_ud;
LL left = 1 + n + up / down;
getRes(left,up%down,down);
}
}
return 0;
}
相关文章推荐
- UVA 10288 Coupons
- UVA 10288 Coupons (概率)
- uva 10288 Coupons 数学期望
- 【UVA】10288-Coupons(概率)
- UVA - 10288 Coupons
- Uva 10288 Coupons (概率dp)
- uva10288 Coupons
- uva 10288 Coupons 数学期望,分数计算表示
- uva 10288 Coupons
- UVa 10288(Coupons) 数学期望(概率+递推) Java
- uva10288 Coupons 【概率 分数】
- UVA 10288 - Coupons
- 【ACM刷题录】UVa10288 Coupons
- UVA10288 Coupons(卡输出神题)
- UVA 题目10288 Coupons(期望)
- UVA 10288 Coupons
- UVa10288 - Coupons
- Uva 10288 - Coupons
- Uva 10288 Coupons
- uva 10288 - Coupons(概率)