#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(i,a,b) for(int (i) = (a); (i) < (b); ++(i)) #define RFOR(i,a,b) for(int (i) = (a)-1; (i) >= (b); --(i)) #define CLEAR(a) memset((a),0,sizeof(a)) #define INF 1000000000 #define PB push_back #define ALL(c) (c).begin(), (c).end() #define pi 2*acos(0.0) #define SQR(a) (a)*(a) #define MP make_pair #define MAX 102 #define MOD 1000000007 typedef long long Int; int n, T, k, c; double P[51][100]; double C[51][51]; double D[51]; pair L[51]; int main() { //freopen("C:\\Users\\���ал�к\\Desktop\\Witaliy\\CodeChef Problems\\Little Elephant and Painting\\input2.txt", "r", stdin); //freopen("C:\\Users\\���ал�к\\Desktop\\Witaliy\\CodeChef Problems\\Little Elephant and Painting\\output2.txt", "w", stdout); FOR (i,0,51) C[i][0] = 1.0; FOR (i,1,51) FOR (j,1,i+1) C[i][j] = C[i-1][j] + C[i-1][j-1]; D[0] = 1.0; FOR (i,1,51) D[i] = D[i-1]*2.0; cin >> T; FOR (t,0,T) { cin >> n >> c >> k; FOR (i,0,k) { int l, r; scanf("%d %d", &l, &r); -- l; -- r; L[i] = MP(l, r); } double res = 0.0; FOR (i,0,n) { memset(P, 0, sizeof(P)); P[0][1] = 1.0; FOR (j,0,k) FOR (w,0,c) { if (P[j][w] < 1E-10) continue; FOR (next,0,c) { int t = (w*next) % c; double p; if (i >= L[j].first && i <= L[j].second) { p = 1.0 / c / 2.0; P[j+1][w] += P[j][w] * p; P[j+1][t] += P[j][w] * p; } else { p = 1.0 / c; P[j+1][w] += P[j][w] * p; } } } FOR (j,0,c) res += j * P[k][j]; } printf("%0.9f\n", res); } return 0; }