Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define task "SPIRAL"
- #include <iostream>
- #include <cstdio>
- using namespace std;
- using ll = long long;
- using ld = long double;
- constexpr int N = 1e5 + 5;
- int n;
- void Read()
- {
- cin >> n;
- }
- void O1_Solution()
- {
- // Ta có 1 * 1 + 2 * 2 + ... + n * n = n * (n + 1) * (2n + 1) / 6
- // Đáp án là n + [n + (n-1) * 2] + [n + (n-1) * 2 + (n-2) * 2] + ... + [n + (n-1) * 2 + (n-2) * 2 + ... + 1 * 2]
- // = n * n + 2 * [(n-1) * (n-1) + (n-2) * (n-2) + ... + 1 * 1]
- // = n * n + 2 * (n - 1) * [2 * (n - 1) + 1] * [(n - 1) + 1] / 6
- // = n * n + (n - 1) * (2 * n - 1) * n / 3
- cout << 1ll * n * n + 1ll * (n - 1) * (2 * n - 1) * n / 3;
- }
- void On_Solution()
- {
- ll a = n, ans = n;
- for (int i = 2; i <= n; ++i)
- {
- a = a + (n - i + 1) * 2;
- ans = ans + a;
- }
- cout << ans;
- }
- int32_t main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- if (fopen(task ".INP", "r"))
- {
- freopen(task ".INP", "r", stdin);
- freopen(task ".OUT", "w", stdout);
- }
- Read();
- // O1_Solution();
- On_Solution();
- }
Advertisement
Add Comment
Please, Sign In to add comment