Coupons UVA

来源:互联网 发布:flash cs6 mac版 编辑:程序博客网 时间:2024/06/14 03:50

推导出最终的式子,然后编程求解即可,注意最大公约数与最小公倍数的使用,具体实现见如下代码:

#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;}

原创粉丝点击