ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1044. Lucky Tickets. Easy!

generate all sums with recursion
Posted by Manciu Ion 21 Jan 2017 01:59
#include <iostream>
#include <stdio.h>
#include <memory.h>

#define NMAX 37
using namespace std;

long cnt[NMAX];
int n;

void genSum(int k, int sum);

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    ///freopen("output.txt", "wt", stdout);
    #endif

    scanf("%d", &n);

    genSum (0 , 0);

    int answer = 0;
    for (int it = 9 * n / 2; it >= 0; --it)
    {
        answer += cnt[it] * cnt[it];
    }

    printf("%d\n", answer);

    return 0;
}

void genSum(int k, int sum)
{
    if (k == n / 2)
    {
        ++cnt[ sum ];
    }
    else
    for (int digit = 0; digit <= 9; ++digit)
    {
        genSum(k + 1, sum + digit);
    }
}