ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1044. Счастливые билеты. Easy!

Brute force Accept in 1.625
Послано zser 8 июн 2025 20:41
#include <bits/stdc++.h>
using namespace std;

int cal(int x){
    int ret = 0;
    while (x > 0){
        ret += x % 10;
        x /= 10;
    }
    return ret;
}

bool lucky(int x, int mm){
    int a = x / mm;
    int b = x % mm;
    if (cal(a) == cal(b)) return true;
    return false;
}

int main(){
    int n;
    cin >> n;
    int nn = round(pow(10, n)) - 1;
    int mm = round(pow(10, n / 2));
    int tot = 0;
    for (int i = 0; i <= nn; i++){
        if (lucky(i, mm)) tot++;
    }
    cout << tot << endl;
}

if your code is simple and fast enough, brute force can work.
Re: Brute force Accept in 1.625
Послано codeJutsu 14 сен 2025 22:58
the input N consists of only even numbers, meaning the number of digits won't be odd.
moreover, input range is limited between 2 and 8, meaning the only possible inputs are 2, 4, 6 and 8.

now this would be my approach :

lets say, the input number is 4 digits in length. meaning, the sum of the first 2 digits need to be equal to the sum of the last 2 digits.

what i'd do is, i'd check for all 2-digit numbers starting from 00 all the way to 99, and store the sum of their digits in an array (in this particular case there are total 100 numbers so that'd be the array length).

after storing the sums in an array, i'd run nested loops through the array. the outer loop would take a sum value from an index. the inner loop would search through all elements in the array from the beginning to the end (including that index from the outer loop too) to see if any sum value matches the outer loop's index sum value. if there's a match, the count increases by 1. the final value of the count is the answer. this process of nested loops is time consuming, so we can sort the array first and then only run the inner loop as long as the next sum values match with the current sum value. this will only improve the performance slightly, as there will still be cases that need the inner loop to run for longer.

a far better approach would be to find the maximum possible value of the sums, and then take an array of that size plus 1. for our example of 4-digit numbers, the maximum possible sum of 2-digit numbers would be 9+9=18 (or 9x2=18) because 99 is the largest 2-digit number. thus the array size here would be 18+1=19 (so that we can access from array[0] all the way to array[18] when needed).

initially, all index values of this array would be 0. as we go through all the 2-digit numbers, we'll find the sum of their digits and raise the value of the corresponding index in the array by 1. this way, we'll immediately know how many times a particular sum has occurred by just looking at that sum's index value, instead of running nested loops. for example, if array[4]=5, then the sum 4 has occurred 5 times in all 2-digit numbers (04, 13, 22, 31, 40). again, if array[5]=6, then the sum 5 has occurred 6 times (05, 14, 23, 32, 41, 50). all we need to do then is find the total sum of all the individual sum count's squares from the entire array.

in case someone doesn't understand why we need to square the individual sum counts : we only counted the sums for 2-digits in our example. but all the possible cases of the 2-digits for a particular sum will occur in both the left half and the right half of a 4-digit number. lets say, the sum value was 4 and a total of 5 cases were found where the 2-digits sum up to 4. then, for each of these 5 cases in one side, all of these 5 cases will occur too in the other side. thus, there will be 5x5=25 total cases where a 4-digit number will have the sum of its left half digits as 4 and the sum of its right half digits as 4 as well. if the sum value was 5, then we'd find 6x6=36 total such 4-digit numbers. that's why we square the individual sum counts before adding them up to the total sum.

there might be even better approaches that i'm not familiar with just yet. maybe an alien would be able to help you with such superior alien concepts.