2015 Sichuan Province Contest (Carries)
2016-03-21 21:44
162 查看
Carries
Time Limit: 1000msMemory Limit: 65536KB
64-bit integer IO format: %lld Java class name:
Main
Type:
None
frog has n
integers a1,a2,…,an,
and she wants to add them pairwise.
Unfortunately, frog is somehow afraid of carries (进位). She defines \emph{hardness}
h(x,y)
for adding x
and y
the number of carries involved in the calculation. For example,
h(1,9)=1,h(1,99)=2.
Find the total hardness adding
n
integers pairwise. In another word, find
∑1≤i<j≤nh(ai,aj)
.
Input
The input consists of multiple tests. For each test:The first line contains
1
integer n
(2≤n≤105).
The second line contains n
integers a1,a2,…,an.
(0≤ai≤109).
Output
For each test, write 1integer which denotes the total hardness.
Sample Input
2 5 5 10 0 1 2 3 4 5 6 7 8 9
Sample Output
1 20
Source
2015 Sichuan Province Contest#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <string> #include <cmath> #include <set> #include <queue> #include <algorithm> #include <vector> const double PI = acos(-1.0); using namespace std; #define esp 1e-8 const int inf = 99999999; const int mod = 1000000007; //freopen("in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取 //freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中 int a[100005]; int main() { int n, i, j; while (~scanf("%d", &n)) { long long s = 0; for (i = 1; i <= n; ++i) scanf("%d", &a[i]); for (i = 1e9; i >= 10; i /= 10)//从高位开始每一位位判断是否会进位 { for (j = 1; j <= n; ++j) { a[j] %= i;//提取当前位以下的值 } sort(a + 1, a + 1 + n); int l = 1, r = n; while (l < r)//二分查找 { if(a[l] + a[r] >= i && l < r) { s += r - l; r --; } while (a[l] + a[r] < i && l < r) { l++; } } } printf("%lld\n", s); } }
相关文章推荐
- Qemu&KVM 第一篇 (3)QEMU 架构
- Windows命令查看文件MD5
- 发现群组(二)分级聚类
- 原型模式(Prototype Pattern)
- 《构建之法》1、2、3章思考与感想
- 如何使用 Microsoft Azure Media Services 现场直播,(Live Streaming) 直播流媒体系统
- java学习笔记4--对象的初始化与回收
- Codeforces IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2)
- 4-5章重点.doc
- 关于学习的态度——循序渐进
- CSS的width:100%和width:auto区别
- 树莓派3安装系统
- 2016年3月21日
- Apple官方文档翻译之iOS 文件管理系统
- 何时应该使用Directive、Controller、Service?
- java学习笔记3--类与对象的基础
- java学习笔记2--数据类型、数组
- ref和out的使用与区别
- (图文)在MyEclipse(2015)中上传项目到github的步骤(很详细)
- 6种排序大集合--由小到大,由大到小!