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
  • 相关阅读:
    挑战编程 uva100 3n+1
    《算法问题实战策略》 BOGGLE
    图论 最短路专辑
    acwing 76. 和为S的连续正数序列
    leetcode 19 删除链表的倒数第N个节点
    水文一篇 汇报下最*的学**况
    acwing 81. 扑克牌的顺子
    Solr基础理论与维护管理快速上手(含查询参数说明)
    Solr基础理论与维护管理快速上手(含查询参数说明)
    利用SolrJ操作solr API完成index操作
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/8059976.html
Copyright © 2011-2022 走看看