SCU 4437 Carries
Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description
Carries
frog has nn integers a1,a2,…,ana1,a2,…,an, and she wants to add them pairwise.
Unfortunately, frog is somehow afraid of carries (进位). She defines hardness h(x,y)h(x,y) for adding xx and yy the number of carries involved in the calculation. For example, h(1,9)=1,h(1,99)=2h(1,9)=1,h(1,99)=2.
Find the total hardness adding nn integers pairwise. In another word, find
∑1≤i<j≤nh(ai,aj)∑1≤i<j≤nh(ai,aj)
.
Input
The input consists of multiple tests. For each test:
The first line contains 11 integer nn (2≤n≤1052≤n≤105). The second line contains nn integers a1,a2,…,ana1,a2,…,an. (0≤ai≤1090≤ai≤109).
Output
For each test, write 11 integer 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
/*/ 题意: 给你n个数,问其中任意两个数相加能够有多少次进位。 例子:1+99=100,个位进位一次,十位进位一次,就是两次; 50+50 十位进位一次,就是一次。 思维题,比较水的题目,一开始没有想到,想到了觉得好脑残的题目。 暴力会超时,直接sort再用lower_bound去找边界值,加上边界值就OK了。 AC代码: /*/
#include"algorithm" #include"iostream" #include"cstring" #include"cstdlib" #include"cstdio" #include"string" #include"vector" #include"stack" #include"queue" #include"cmath" #include"map" using namespace std; typedef long long LL ; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define FK(x) cout<<"["<<x<<"]\n" #define memset(x,y) memset(x,y,sizeof(x)) #define memcpy(x,y) memcpy(x,y,sizeof(x)) #define bigfor(T) for(int qq=1;qq<= T ;qq++) const int MX=1e5+1e3; LL num[MX],temp[MX]; int main(){ int n; while(~scanf("%d",&n)){ LL ans=0,mod=1; for(int i=1;i<=n;i++){ scanf("%lld",&num[i]); } for(int i=1;i<=9;i++){ mod*=10; for(int i=1;i<=n;i++)temp[i]=num[i]%mod; //按照个位十位百位千位轮着保存所有的数。 sort(temp+1,temp+n+1);//排序 for(int i=1;i<=n;i++)ans+=n-(lower_bound(temp+i+1,temp+n+1,mod-temp[i])-temp)+1; //如果某个数比此时mod-目标数大,那么这个数加上目标数就会进一位,把这些数个数求和。 } printf("%lld\n",ans); } return 0; }
时间: 2024-10-29 19:12:12