Dang_Quan_10_Tin

SPIRAL TS10 PTNK 2018-2019

Jan 6th, 2022
1,214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #define task "SPIRAL"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5.  
  6. using namespace std;
  7.  
  8. using ll = long long;
  9. using ld = long double;
  10.  
  11. constexpr int N = 1e5 + 5;
  12. int n;
  13.  
  14. void Read()
  15. {
  16.     cin >> n;
  17. }
  18.  
  19. void O1_Solution()
  20. {
  21.     // Ta có 1 * 1 + 2 * 2 + ... + n * n = n * (n + 1) * (2n + 1) / 6
  22.     // Đáp án là   n + [n + (n-1) * 2] + [n + (n-1) * 2 + (n-2) * 2] + ... + [n + (n-1) * 2 + (n-2) * 2 + ... + 1 * 2]
  23.     //           = n * n + 2 * [(n-1) * (n-1) + (n-2) * (n-2) + ... + 1 * 1]
  24.     //           = n * n + 2 * (n - 1) * [2 * (n - 1) + 1] * [(n - 1) + 1] / 6
  25.     //           = n * n + (n - 1) * (2 * n - 1) * n / 3
  26.  
  27.     cout << 1ll * n * n + 1ll * (n - 1) * (2 * n - 1) * n / 3;
  28. }
  29.  
  30. void On_Solution()
  31. {
  32.     ll a = n, ans = n;
  33.  
  34.     for (int i = 2; i <= n; ++i)
  35.     {
  36.         a = a + (n - i + 1) * 2;
  37.         ans = ans + a;
  38.     }
  39.  
  40.     cout << ans;
  41. }
  42.  
  43. int32_t main()
  44. {
  45.     ios::sync_with_stdio(0);
  46.     cin.tie(0);
  47.     cout.tie(0);
  48.     if (fopen(task ".INP", "r"))
  49.     {
  50.         freopen(task ".INP", "r", stdin);
  51.         freopen(task ".OUT", "w", stdout);
  52.     }
  53.  
  54.     Read();
  55.     // O1_Solution();
  56.     On_Solution();
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment