您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: