当前位置:   article > 正文

Caddi Programming Contest 2021(AtCoder Beginner Contest 193) ----- C(思维)_given is a number sequence aa of length nn. find t

given is a number sequence aa of length nn. find the sum of squared differen

 

C - Squared Error 


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 300300 points

Problem Statement

Given is a number sequence AA of length NN.
Find the sum of squared differences of every pair of elements: N∑i=2i−1∑j=1(Ai−Aj)2∑i=2N∑j=1i−1(Ai−Aj)2.

Constraints

  • 2≤N≤3×1052≤N≤3×105
  • |Ai|≤200|Ai|≤200
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

  1. NN
  2. A1A1 A2A2 A3A3 ⋯⋯ ANAN

Output

Print the answer.


Sample Input 1 Copy

Copy

  1. 3
  2. 2 8 4

Sample Output 1 Copy

Copy

56

We have ∑Ni=2∑i−1j=1(Ai−Aj)2=(8−2)2+(4−2)2+(4−8)2=56∑i=2N∑j=1i−1(Ai−Aj)2=(8−2)2+(4−2)2+(4−8)2=56.


Sample Input 2 Copy

Copy

  1. 5
  2. -5 8 9 -4 -3

Sample Output 2 Copy

Copy

950

 

 

题解:
 

code:
 

  1. #include<bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3. #define N 100003
  4. #define mod 100000
  5. #define pb push_back
  6. #define x first
  7. #define y second
  8. #define ull unsigned long long
  9. #define ll long long
  10. using namespace std;
  11. set<ll>s;
  12. int main()
  13. {
  14. ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  15. //freopen("in.txt", "r", stdin);
  16. //freopen("out.txt", "w", stdout);
  17. ll n;
  18. cin >> n;
  19. for(ll i = 2;i * i <= n;i++)
  20. {
  21. for(ll j = 2;;j++)
  22. {
  23. ll tmp = pow(i,j);
  24. if(tmp <= n)
  25. s.insert(tmp);
  26. else
  27. break;
  28. }
  29. }
  30. cout << n - s.size() << endl;
  31. return 0;
  32. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/52703
推荐阅读
相关标签
  

闽ICP备14008679号