F - Tickets Gym - 101911F (思维)

F - Tickets Gym - 101911F (思维)

Last nn days Monocarp used public transport to get to the university. He received a ticket with number titi during the ii-th day.

Tickets' numbers are six digit non-negative integers with possible leading zeroes. For example, 123456123456, 000000000000, 099999099999, 999999999999 are correct ticket numbers, but 12345671234567, 1234512345, 99 are not. Every day tickets are numbered from zero. The first passenger gets ticket number 000000000000, the second one — ticket number 000001000001, the third one — number 000002000002 and so on every day. Assume that each day the number of passengers doesn't exceed 106106.

Unluckiness of the ticket is equal to absolute difference between the sum of the first three digits and the sum of the last three. For example, unluckiness of the ticket number 345123345123 is equal to |(3+4+5)−(1+2+3)|=6|(3+4+5)−(1+2+3)|=6, or unluckiness of the ticket number 238526238526 is equal to |(2+3+8)−(5+2+6)|=0|(2+3+8)−(5+2+6)|=0.

One passenger is luckier than other if unluckiness of first passenger's ticket is strictly less than unluckiness of the second one's ticket.

For each of nn days for given Monocarp's ticket's number titi calculate the number of passengers who received their tickets before him during this day and are luckier than Monocarp.

Examine examples for the further understanding of the statement.

Input

The first line contains one integer nn (1≤n≤2⋅105)(1≤n≤2⋅105) — the number of days during which Monocarp used public transport.

Each of the next nn lines contains one six digit integer titi (0≤ti<1060≤ti<106, titi can have leading zeroes) — the ticket Monocarp received during the corresponding day.

Output

Print nn lines: one integer per line — the number of passengers who received their tickets before Monocarp during the corresponding day and are luckier than Monocarp.

Example

Input

5
001000
000000
999000
453234
654331

Output

1
0
998999
121496
470362

Note

During the first day the only one passenger who got ticket before Monocarp was luckier. This passenger got ticket number 000000000000.

During the second day Monocarp was the first one, so there was nobody before him.

During the third day all passengers except one who got tickets before Monocarp were more luckier than him. The one whose unluckiness was equal to Monocarp's unluckiness got ticket number 000999000999.

 

题意:给你一个6位数x,x的前三位之和与后三位之和的绝对值之差记为X。求6位数y,y的前三位之和与后三位之和的绝对值之差记为Y。当x>y并且X>Y时满足条件的Y的个数。

思路:提前预处理。

 

代码:

#include <bits/stdc++.h>

using namespace std;

int ans[1000005];
int f[30];

int main()
{
    memset(ans,0,sizeof(ans));
    memset(f,0,sizeof(f));
    for ( int i=0; i<1000000; i++ ) {
        int t = abs( i/100000+i/10000%10+i/1000%10-i/100%10-i/10%10-i%10 );
        f[t] ++;

        for ( int j=0; j<t; j++ ) {
            ans[i] += f[j];
        }
    }

    int listt,a;
    cin >> listt;
    while ( listt-- ) {
        cin >> a;
        cout << ans[a] << endl;
    }


    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值