zoukankan      html  css  js  c++  java
  • 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]<<'
    ';
        }
    } 

    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
  • 相关阅读:
    mysql把查询结果集插入到表理
    js遍历json数据
    php事务回滚
    win10定时执行php脚本
    php输出json的内容
    图像的几个基本概念
    linux系统编程之I/O内核数据结构
    linux系统编程之错误处理
    深拷贝和浅拷贝
    mysql用户的创建
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/8059976.html
Copyright © 2011-2022 走看看