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 chinese,
Russianand
Vietnameseas 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.
For each test case, print the answer on a separate line.
Subtask #1 (15 points):
- Input:
- 3
- 1
- 2
- 3
- Output:
- 2
- 12
- 36
来源: http://www.bubuko.com/infodetail-2433650.html