Блог пользователя PoIycarp

Автор PoIycarp, история, 3 месяца назад, По-английски

So I'm trying to solve this problem but my solution has O(n^6) complexity and I don't know how you can do this faster.

int ans = 0;
for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
        for(int k = 0; k < n; k++){
            for(int x = 0; x < n; x++){
                for(int y = 0; y < n; y++){
                     for(int z = 0; z < n; z++){
                         if(i + j + k == x + y + z) ans++;
                     }
                 }
            }
        }
    }
}
cout << ans << "\n";

I looked up the sequence on OEIS but there isn't any other relevant information.
https://oeis.org/search?q=1+%2C+20+%2C+141+%2C+580+%2C+1751&language=english&go=Search
I need to calculate this fast for n <= 100000.

Полный текст и комментарии »

  • Проголосовать: нравится
  • -12
  • Проголосовать: не нравится