codeforces340C
Tourist Problem
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.
Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.
The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
Input
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 107).
Output
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
Examples
3
2 3 5
22 3
Note
Consider 6 possible routes:
- [2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
- [2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
- [3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
- [3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
- [5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
- [5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
The average travel distance is =
=
.
sol:小学奥数题.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n;
ll a[N],ans=;
ll Jiec[N],Invj[N];
inline ll gcd(ll a,ll b)
{
return (b==)?a:(gcd(b,a%b));
}
int main()
{
int i;
ll S=,GG;
R(n);
for(i=;i<=n;i++) R(a[i]);
sort(a+,a+n+);
for(i=;i<=n;i++)
{
S+=a[i-];
ans+=1ll*(i-)*a[i]-S;
}
S+=a[n];
ans=1ll*(ans*2ll+S);
GG=gcd(ans,n);
W(ans/GG); Wl(n/GG);
return ;
}
/*
Input
3
2 3 5
Output
22 3 Input
4
1 5 77 2
Output
547 4 Input
40
8995197 7520501 942559 8012058 3749344 3471059 9817796 3187774 4735591 6477783 7024598 3155420 6039802 2879311 2738670 5930138 4604402 7772492 6089337 317953 4598621 6924769 455347 4360383 1441848 9189601 1838826 5027295 9248947 7562916 8341568 4690450 6877041 507074 2390889 8405736 4562116 2755285 3032168 7770391
Output
644565018 5
*/
codeforces340C的更多相关文章
随机推荐
- javascript——加强for循环 和Java中的加强for循环的区别
javascript中获得的是下标 in var id=[4,5,6]; for (var index in id) { console.log(id[index]); } Java中获得的 ...
- tftp服务器的搭建和使用
一.tftp介绍 TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,==提供不复 ...
- 从零开始搭建react应用
用create-react-app搭建react应用,了解npm run start的工作过程. 第一步:安装脚手架 create-react-app 1. 在node里 npm install cr ...
- VUE神速搭建项目
1.npm install -g vue-cli 全局安装vue-cli脚手架 2.vue init webpack vueTest 初始化一个基于webpack的项目 3.cd vueTest 进入 ...
- OSCP-Kioptrix2014-1 环境搭建
环境搭建 该系列文章参考 : https://www.youtube.com/watch?v=bWM0BCQ5q1o&list=PL9WW-prbqvGzHsGK_OqTyYWbCZjucpI ...
- Centos7下查看端口占用
netstat -nap #会列出所有正在使用的端口及关联的进程/应用 netstat -lnp|grep 5000 这条语句的作用是查询占用5000端口的应用和进程,把5000端口替换成你要过滤的端 ...
- axiso基本使用及python接收处理
安装$ npm install axios 1.发送get请求: axios.get("/api/v1.0/cars?id=132").then(function(res){ co ...
- VS2012隐藏输出窗口的快捷键是什么。
纯属用键盘无法直接关闭这个窗口.有一个变通的方法是,先切换到这个输出窗口(标题呈现高亮的蓝色),使用Alt+W打开窗口菜单,选H隐藏就可以关闭.使用Ctrl+Alt+o可再次打开.按ESC就可以了.我 ...
- 中断或取消Promise链的可行方案
ES6标准引入的异步编程解决方案Promise,能够将层层嵌套的回调转化成扁平的Promise链式调用,优雅地解决了“回调地狱”的问题.当Promise链中抛出一个错误时,错误信息沿着链路向后传递,直 ...
- Win10应用商店缓存信息多如何去清理?
Win10系统的应用商店相比之前有了许多的更新,微软也成立了专门的团队准备对应用商店进行完善,但是我们在使用应用商店的过程中会产生许多缓存文件,占用电脑空间资源,也会影响电脑的运行速度. 下面好系统重 ...