fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6. ios::sync_with_stdio(false);
  7. cin.tie(nullptr);
  8. int n, c, m;
  9. cin >> n >> c >> m;
  10. vector<int> cnt(n + 1);
  11. for (int i = 0; i < m; i++) {
  12. int l, r;
  13. cin >> l >> r;
  14. cnt[l - 1]++;
  15. cnt[r]--;
  16. }
  17. for (int i = 0; i < n - 1; i++) {
  18. cnt[i + 1] += cnt[i];
  19. }
  20. vector<vector<double>> f(m + 1, vector<double>(c));
  21. f[0][1] = 1.0;
  22. for (int i = 0; i < m; i++) {
  23. for (int a = 0; a < c; a++) {
  24. f[i + 1][a] += f[i][a] * 0.5; // 不涂色
  25. for (int b = 0; b < c; b++) {
  26. f[i + 1][a * b % c] += f[i][a] * 0.5 / c; // 涂了颜色
  27. }
  28. }
  29. }
  30. double ans = 0.0;
  31. for (int i = 0; i < n; i++) {
  32. int cc = cnt[i];
  33. for (int j = 0; j < c; j++) {
  34. ans += f[cc][j] * j;
  35. }
  36. }
  37. cout << fixed << setprecision(3) << ans << '\n';
  38. cerr << "Time: " << double(clock()) / CLOCKS_PER_SEC << '\n';
  39. return 0;
  40. }
  41.  
Success #stdin #stdout #stderr 0s 4464KB
stdin
3 3 3
1 2
2 3
1 3
stdout
2.639
stderr
Time: 0.004982