Advertisement
a53

prosum

a53
Jan 18th, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <map>
  4. #include <algorithm>
  5. #include <set>
  6.  
  7. #define DIM 100010
  8. #define ULL long long
  9. using namespace std;
  10. ULL v[DIM], w[DIM];
  11. ULL n, m, i, j, sol, X, Y, nr0;
  12.  
  13. map<ULL, ULL> mp, f;
  14. map<ULL, ULL>::iterator it;
  15. set<ULL> st;
  16.  
  17.  
  18.  
  19. ULL cmmdc(ULL a, ULL b, ULL &x, ULL &y) {
  20. if (b == 0) {
  21. x = 1;
  22. y = 0;
  23. return a;
  24. } else {
  25. ULL xa, ya;
  26. ULL r = cmmdc(b, a%b, xa, ya);
  27. x = ya;
  28. y = xa - (a/b)*ya;
  29. return r;
  30. }
  31. }
  32.  
  33. ULL gcd(ULL a, ULL b) {
  34. if (b == 0)
  35. return a;
  36. else
  37. return gcd(b, a%b);
  38. }
  39.  
  40. int main () {
  41. ifstream fin ("prosum.in");
  42. ofstream fout("prosum.out");
  43. fin>>n>>m;
  44. for (i=1;i<=n;i++) {
  45. fin>>v[i];
  46. v[i] %= m;
  47. w[i] = gcd(v[i]+1, m);
  48. st.insert(v[i]);
  49. }
  50.  
  51. for (i=1;i<=n;i++) {
  52. if (v[i] == 0) {
  53. nr0++;
  54. continue;
  55. }
  56. if (w[i] != 1)
  57. continue;
  58. cmmdc((v[i]+1)%m, m, X, Y);
  59. X = (X%m+m)%m;
  60. X--;
  61. if (X < 0)
  62. X += m;
  63. if (st.find(X) != st.end()) {
  64. f[v[i]] = X;
  65. f[X] = v[i];
  66. }
  67. }
  68.  
  69.  
  70. for (i=1;i<=n;i++) {
  71. if (f.find(v[i]) != f.end()) {
  72. it = mp.find(f[ v[i] ]);
  73. if (it != mp.end()) {
  74. sol += mp[f[ v[i] ]];
  75. }
  76. }
  77. it = mp.find(v[ i ]);
  78. if (it == mp.end()) {
  79. mp[v[i]] = 1;
  80.  
  81. } else {
  82. mp[v[i]]++;
  83. }
  84. }
  85. fout<<sol + nr0*(nr0-1)/2;
  86.  
  87. return 0;
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement