CF&&CC百套计划2 CodeChef December Challenge 2017 Total Diamonds

https://www.codechef.com/DEC17/problems/VK18

 

#include<cstdio>
#include<iostream>
#include<algorithm>
 
using namespace std;
 
#define N 1000001
 
long long sum[N*2],dp[N];
 
int num[11];
 
void read(int &x)
{
    x=0; char c=getchar();
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); }
}
 
int main()
{
    int odd,even;
    int x,len;
    int m=N-1<<1;
    for(int i=1;i<=m;++i)    
    {
        odd=even=0;
        x=i; 
        len=0;
        while(x) num[++len]=x%10,x/=10;
        for(int j=1;j<=len;++j)
        {
            if(num[j]&1) odd+=num[j];
            else even+=num[j];
        }
        sum[i]=abs(odd-even)+sum[i-1];
    }
    for(int i=1;i<N;++i) dp[i]=dp[i-1]+(sum[i*2]-sum[i])*2-(sum[i*2]-sum[i*2-1]);
    int T,n;
    read(T);
    while(T--)
    {
        read(n);
        cout<<dp[n]<<'\n';
    }
} 

 

Read problems statements in Mandarin chineseRussian andVietnamese as well.

Chef is so good at programming that he won almost all competitions. With all the prizes, Chef bought a new house. The house looks like a grid of size N (1-indexed) which consists of N × N rooms containing diamonds. For each room, the room number is equal to the sum of the row number and the column number.

The number of diamonds present in each room is equal to the absolute difference between the sum of even digits and sum of odd digits in its room number. For example, if the room number is 3216, then the number of diamonds present in that room will be |(2+6)-(3+1)| = 4.

You are given the number N. You have to print the total number of diamonds present in Chef's house.

Input

 

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The only line of each test case contains a single integer N.

 

Output

For each test case, print the answer on a separate line.

Constraints

  • 1 ≤ T ≤ 105
  • 1 ≤ N ≤ 106

Subtasks

Subtask #1 (15 points):

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 1000

 

Subtask #2 (15 points):

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 106

 

Subtask #3 (70 points): original constraints

Example

Input:

3
1
2
3

Output:

2
12
36

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/8059976.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值