题解:CF1822C Bun Lover

· · 题解

思路

将巧克力酱分为两部分:边框和内部。边框的长度显而易见是 4n,内部需要打表。 n 4 5 6
长度 10 17 26

打表后观察可得,内部的公式为 (n - 1) ^ 2 + 1。化简为 n ^ 2 - 2n + 2。合起来就是 n ^ 2 + 2n + 2

代码

#include <bits/stdc++.h>
using namespace std;
long long t,n;
signed main()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> t;
    while(t--)
    {
        cin >> n;
        cout << n * n + 2 * n + 2 << '\n';
    }
    return 0;
}