当前位置:   article > 正文

Exam in MAC [容斥]

Exam in MAC [容斥]

题意

思路

正难则反

反过来需要考虑的是:

(1) 所有满条件一的(x,y)有多少对:

x = 0 时,有c+1对   x = 1 时,有c对  ......  x = c 时,有1对

以此类推 一共有 (c+2)(c+1)/2 对

(2) 符合 x + y ∈ S的有多少对:

那就是x + y = ai  对 x = 0 而言 y有固定的一种取值ai

但是要保证的是 y <= c 所以有 c - x + 1

而x: 0->ai 所以对每一个ai有 ai/2 + 1种

(3) 符合 y - x ∈ S 的有多少对:

y - x = ai 

那么 y = ai + x < c

y ∈ [x,c-a[i]] 而x是从0开始的 所以每一个ai 都有 c - a[i] + 1 种的情况

(4) 符合 (2) + (3)的有多少对:

x + y = ai

y - x = aj

y = (ai + aj) / 2 所以要保证的是只有同奇偶性 才有这种情况

比如 奇数有{1,3,5}那么其实是C(2,3) + 3 那就是 n(n-1)/2 + n = n(n+1)/2

答案就是 odd(odd+1) / 2 + even(even+1) / 2

(LAST ) ALL IN ALL

根据容斥定理 res = (1) - (2) - (3) + (4)

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<stack>
  4. #include<vector>
  5. #include<algorithm>
  6. #include<cmath>
  7. #include<cstring>
  8. #include<map>
  9. #include<set>
  10. #include<vector>
  11. #define int long long
  12. #define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
  13. #define PI acos(-1.0)
  14. using namespace std;
  15. typedef long long ll;
  16. typedef pair<int,int> PII;
  17. const int INF = 1e18 + 10;
  18. const int N = 1e5 + 10;
  19. const int M = 1e7 + 10;// 节点数量 3e6就够了是为什么?
  20. const int mod = 1e9 + 7;
  21. int n, m, k, x, now, ans;
  22. int qcal(int a, int b) { int res = 1; while (b) { if (b & 1) res = res * a % mod; b >>= 1; a = a * a % mod; } return res; }
  23. int a[N], b[N];
  24. bool is_prime(int n){if (n < 2) return false; for (int i = 2; i <= n / i; i++){if (n % i == 0){return false;}}return true;}
  25. void gzy()
  26. {
  27. int c;
  28. cin >> n >> c;
  29. int ans = (1 + c) * (2 + c) / 2;
  30. int odd = 0,even = 0;
  31. for(int i = 1;i <= n;i ++)
  32. {
  33. cin >> x;
  34. ans -= x / 2 + 1;
  35. ans -= (c - x + 1);
  36. if(x % 2 == 0) odd ++;
  37. else even ++;
  38. }
  39. ans += (odd + 1) * odd / 2;
  40. ans += (even + 1) * even / 2;
  41. cout << ans << endl;
  42. }
  43. signed main()
  44. {
  45. int _ = 1; cin >> _;
  46. while (_--) gzy();
  47. return 0;
  48. }
  49. // /**
  50. // *  ┏┓   ┏┓+ +
  51. // * ┏┛┻━━━┛┻┓ + +
  52. // * ┃       ┃
  53. // * ┃   ━   ┃ ++ + + +
  54. // * ████━████+
  55. // * ◥██◤ ◥██◤ +
  56. // * ┃   ┻   ┃
  57. // * ┃       ┃ + +
  58. // * ┗━┓   ┏━┛
  59. // *   ┃   ┃ + + + +Code is far away from  
  60. // *   ┃   ┃ + bug with the animal protecting
  61. // *   ┃    ┗━━━┓ 神兽保佑,代码无bug 
  62. // *   ┃       ┣┓
  63. // *   ┃        ┏┛
  64. // *  ┗┓┓┏━┳┓┏┛ + + + +
  65. // *    ┃┫┫ ┃┫┫
  66. // *    ┗┻┛ ┗┻┛+ + + +
  67. // */

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号