额...水题...坑就是==k!!
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 //#include <bits/stdc++.h> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 typedef long long LL; 6 const double eps=1e-2; 7 const int mod=10056; 8 const int N=1e6+5; 9 #define lson l, m, rt<<1 10 #define rson m+1, r, rt<<1|1 11 12 struct node 13 { 14 int p, c; 15 }a[10005]; 16 bool cmp(node a, node b) 17 { 18 if(a.c==b.c) 19 return a.p<b.p; 20 return a.c<b.c; 21 } 22 int main() 23 { 24 int t; 25 scanf("%d", &t); 26 while(t--) 27 { 28 int n, x, k; 29 scanf("%d%d%d", &n, &x, &k); 30 for(int i=0;i<n;i++) 31 scanf("%d%d", &a[i].p, &a[i].c); 32 sort(a, a+n, cmp); 33 bool flag=0; 34 for(int i=0;i<n;i++) 35 { 36 if(a[i].c==k && a[i].p>=x) 37 { 38 flag=1; 39 break; 40 } 41 if(a[i].c>k) 42 break; 43 } 44 if(flag) 45 puts("Yes"); 46 else 47 puts("No"); 48 } 49 return 0; 50 }
额...也是水题...
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 //#include <bits/stdc++.h> 2 #include <cstdio> 3 #include <string> 4 #include <cstring> 5 #include <iostream> 6 #include <sstream> 7 #include <algorithm> 8 using namespace std; 9 typedef long long LL; 10 const double eps=1e-2; 11 const int mod=10056; 12 const int N=1e6+5; 13 #define lson l, m, rt<<1 14 #define rson m+1, r, rt<<1|1 15 16 char s[]={"marshtomp"}; 17 char ss[205]; 18 bool is(int z) 19 { 20 for(int i=0;i<9;i++) 21 if(toupper(ss[i+z])!=toupper(s[i])) 22 return false; 23 return true; 24 } 25 int main() 26 { 27 while(gets(ss)) 28 { 29 int n=strlen(ss); 30 for(int i=0;i<n;i++) 31 { 32 if(is(i)) 33 printf("fjxmlhx"), i+=8; 34 else 35 printf("%c", ss[i]); 36 } 37 puts(""); 38 } 39 return 0; 40 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 //#include <bits/stdc++.h> 2 #include <cstdio> 3 #include <string> 4 #include <cmath> 5 #include <cstring> 6 #include <iostream> 7 #include <sstream> 8 #include <algorithm> 9 using namespace std; 10 #include <vector> 11 typedef long long LL; 12 const double eps=1e-2; 13 const LL mod=1000000009; 14 const int N=1e5+5; 15 #define lson l, m, rt<<1 16 #define rson m+1, r, rt<<1|1 17 const double E=2.718281828459; 18 19 const int MAXN=100005; 20 int a[MAXN]; 21 int dp1[MAXN][20]; 22 int dp2[MAXN][20]; 23 int mm[MAXN]; 24 void initRMQ(int n) 25 { 26 mm[0]=-1; 27 for(int i=1;i<=n;i++) 28 { 29 mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1]; 30 // dp[i][0]=b[i]; 31 dp1[i][0]=dp2[i][0]=i; 32 } 33 for(int j=1;j<=mm[n];j++) 34 for(int i=1;i+(1<<j)-1<=n;i++) 35 { 36 dp1[i][j]=a[dp1[i][j-1]]<a[dp1[i+(1<<(j-1))][j-1]]? dp1[i][j-1]:dp1[i+(1<<(j-1))][j-1]; 37 dp2[i][j]=a[dp2[i][j-1]]>a[dp2[i+(1<<(j-1))][j-1]]? dp2[i][j-1]:dp2[i+(1<<(j-1))][j-1]; 38 } 39 } 40 int rmq1(int x,int y) 41 { 42 int k=mm[y-x+1]; 43 return a[dp1[x][k]]<a[dp1[y-(1<<k)+1][k]]?dp1[x][k]:dp1[y-(1<<k)+1][k]; 44 } 45 int rmq2(int x,int y) 46 { 47 int k=mm[y-x+1]; 48 return a[dp2[x][k]]>a[dp2[y-(1<<k)+1][k]]?dp2[x][k]:dp2[y-(1<<k)+1][k]; 49 } 50 vector<int> ans; 51 int main() 52 { 53 int n; 54 while(~scanf("%d",&n)) 55 { 56 for(int i=1;i<=n;i++) 57 scanf("%d", &a[i]); 58 initRMQ(n); 59 ans.clear(); 60 ans.push_back(1); 61 int cur=1; 62 while(cur<n) 63 { 64 int l=cur, r=n, tt=cur; 65 while(l<=r) 66 { 67 int mid=(l+r)>>1; 68 int id1=rmq1(cur, mid); 69 int id2=rmq2(cur, mid); 70 if(a[id1]<a[cur] && a[id2]>a[cur]) 71 r=mid-1; 72 else 73 tt=mid, l=mid+1; 74 } 75 int id1=rmq1(cur, tt); 76 int id2=rmq2(cur, tt); 77 if(a[cur]==a[id1]) 78 ans.push_back(id2), cur=id2; 79 else 80 ans.push_back(id1), cur=id1; 81 } 82 printf("%d\n", (int)ans.size()); 83 for(int i=0;i<(int)ans.size();i++) 84 { 85 printf("%d", ans[i]); 86 if(i==ans.size()-1) 87 puts(""); 88 else 89 printf(" "); 90 } 91 } 92 return 0; 93 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 //#include <bits/stdc++.h> 2 #include <cstdio> 3 #include <string> 4 #include <cmath> 5 #include <cstring> 6 #include <iostream> 7 #include <sstream> 8 #include <algorithm> 9 using namespace std; 10 #include <vector> 11 typedef long long LL; 12 const double eps=1e-2; 13 const LL mod=1000000009; 14 const int N=1e5+5; 15 #define lson l, m, rt<<1 16 #define rson m+1, r, rt<<1|1 17 const double E=2.718281828459; 18 19 LL dp[100005]; 20 int main() 21 { 22 int t; 23 scanf("%d", &t); 24 while(t--) 25 { 26 memset(dp, 0, sizeof(dp)); 27 dp[0]=1; 28 int k, n; 29 scanf("%d%d", &k, &n); 30 if(k>=n) 31 k=n; 32 for(int i=1;i<=n;i<<=1) 33 for(int j=n;j>=i;j--) 34 { 35 LL tmp=0; 36 for(int l=0;l<=k;l++) 37 if(j-l*i<0) 38 break; 39 else 40 tmp+=dp[j-l*i]; 41 dp[j]=tmp%mod; 42 } 43 printf("%lld\n", dp[n]); 44 } 45 return 0; 46 }
与HDOJ的 4986 一样
明显的打表题,但是真的打表会超内存,于是就有了几种方法:
1.个人比较赞同 分段打表法
比较容易想 也不用化公式,比较无脑的方法
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 long double biao[1001]={0, 14.392727, 15.085874, 15.491339, 15.779021, 16.002164, 16.184486, 16.338636, 16.472168, 16.589951, 16.695311, 16.790622, 16.877633, 16.957676, 17.031784, 17.100776, 17.165315, 17.225940, 17.283098, 17.337165, 17.388459, 17.437249, 17.483769, 17.528220, 17.570780, 17.611602, 17.650823, 17.688563, 17.724931, 17.760022, 17.793924, 17.826713, 17.858462, 17.889234, 17.919087, 17.948074, 17.976245, 18.003644, 18.030312, 18.056288, 18.081606, 18.106298, 18.130396, 18.153926, 18.176916, 18.199389, 18.221368, 18.242874, 18.263927, 18.284547, 18.304749, 18.324552, 18.343970, 18.363018, 18.381710, 18.400059, 18.418078, 18.435777, 18.453169, 18.470264, 18.487071, 18.503600, 18.519861, 18.535861, 18.551609, 18.567114, 18.582381, 18.597419, 18.612234, 18.626833, 18.641221, 18.655406, 18.669392, 18.683186, 18.696791, 18.710214, 18.723460, 18.736532, 18.749435, 18.762174, 18.774753, 18.787175, 18.799445, 18.811567, 18.823543, 18.835377, 18.847074, 18.858634, 18.870063, 18.881363, 18.892536, 18.903586, 18.914515, 18.925326, 18.936021, 18.946603, 18.957074, 18.967437, 18.977694, 18.987846, 18.997896, 19.007847, 19.017699, 19.027455, 19.037117, 19.046687, 19.056165, 19.065555, 19.074857, 19.084074, 19.093207, 19.102256, 19.111225, 19.120114, 19.128925, 19.137658, 19.146316, 19.154900, 19.163411, 19.171850, 19.180218, 19.188517, 19.196747, 19.204911, 19.213008, 19.221040, 19.229008, 19.236913, 19.244756, 19.252539, 19.260261, 19.267924, 19.275528, 19.283075, 19.290566, 19.298001, 19.305381, 19.312707, 19.319980, 19.327200, 19.334369, 19.341486, 19.348553, 19.355571, 19.362540, 19.369460, 19.376333, 19.383159, 19.389939, 19.396673, 19.403362, 19.410006, 19.416607, 19.423164, 19.429679, 19.436151, 19.442582, 19.448972, 19.455321, 19.461630, 19.467900, 19.474131, 19.480323, 19.486476, 19.492593, 19.498672, 19.504714, 19.510720, 19.516690, 19.522625, 19.528525, 19.534390, 19.540221, 19.546018, 19.551782, 19.557512, 19.563210, 19.568876, 19.574510, 19.580112, 19.585683, 19.591223, 19.596733, 19.602212, 19.607662, 19.613082, 19.618473, 19.623835, 19.629168, 19.634473, 19.639750, 19.645000, 19.650222, 19.655416, 19.660584, 19.665726, 19.670841, 19.675930, 19.680993, 19.686031, 19.691044, 19.696031, 19.700994, 19.705932, 19.710846, 19.715736, 19.720602, 19.725445, 19.730264, 19.735060, 19.739834, 19.744584, 19.749312, 19.754018, 19.758702, 19.763364, 19.768005, 19.772624, 19.777221, 19.781798, 19.786354, 19.790889, 19.795404, 19.799898, 19.804372, 19.808827, 19.813261, 19.817676, 19.822072, 19.826448, 19.830806, 19.835144, 19.839464, 19.843765, 19.848047, 19.852312, 19.856558, 19.860786, 19.864997, 19.869190, 19.873365, 19.877523, 19.881664, 19.885788, 19.889894, 19.893984, 19.898058, 19.902115, 19.906155, 19.910179, 19.914187, 19.918179, 19.922155, 19.926116, 19.930060, 19.933990, 19.937904, 19.941802, 19.945686, 19.949554, 19.953408, 19.957247, 19.961071, 19.964880, 19.968675, 19.972456, 19.976223, 19.979975, 19.983713, 19.987438, 19.991148, 19.994845, 19.998528, 20.002198, 20.005854, 20.009497, 20.013127, 20.016744, 20.020347, 20.023938, 20.027516, 20.031081, 20.034633, 20.038173, 20.041700, 20.045215, 20.048718, 20.052208, 20.055687, 20.059153, 20.062607, 20.066049, 20.069480, 20.072899, 20.076306, 20.079702, 20.083086, 20.086458, 20.089820, 20.093170, 20.096509, 20.099836, 20.103153, 20.106459, 20.109754, 20.113038, 20.116311, 20.119574, 20.122826, 20.126068, 20.129299, 20.132519, 20.135729, 20.138929, 20.142119, 20.145299, 20.148468, 20.151628, 20.154778, 20.157917, 20.161047, 20.164167, 20.167278, 20.170379, 20.173470, 20.176551, 20.179624, 20.182686, 20.185740, 20.188784, 20.191819, 20.194845, 20.197861, 20.200869, 20.203867, 20.206857, 20.209837, 20.212809, 20.215772, 20.218726, 20.221672, 20.224609, 20.227537, 20.230457, 20.233368, 20.236271, 20.239165, 20.242051, 20.244929, 20.247798, 20.250659, 20.253512, 20.256357, 20.259194, 20.262023, 20.264844, 20.267657, 20.270462, 20.273259, 20.276049, 20.278830, 20.281604, 20.284370, 20.287129, 20.289880, 20.292624, 20.295360, 20.298088, 20.300809, 20.303523, 20.306229, 20.308928, 20.311620, 20.314305, 20.316982, 20.319652, 20.322315, 20.324971, 20.327620, 20.330262, 20.332897, 20.335526, 20.338147, 20.340761, 20.343369, 20.345970, 20.348564, 20.351151, 20.353732, 20.356306, 20.358873, 20.361434, 20.363988, 20.366536, 20.369077, 20.371612, 20.374140, 20.376663, 20.379178, 20.381688, 20.384191, 20.386688, 20.389178, 20.391663, 20.394141, 20.396613, 20.399079, 20.401539, 20.403993, 20.406441, 20.408883, 20.411319, 20.413750, 20.416174, 20.418592, 20.421005, 20.423411, 20.425812, 20.428208, 20.430597, 20.432981, 20.435359, 20.437732, 20.440098, 20.442460, 20.444815, 20.447166, 20.449510, 20.451849, 20.454183, 20.456511, 20.458834, 20.461152, 20.463464, 20.465771, 20.468072, 20.470368, 20.472659, 20.474945, 20.477226, 20.479501, 20.481771, 20.484036, 20.486296, 20.488551, 20.490801, 20.493045, 20.495285, 20.497519, 20.499749, 20.501974, 20.504194, 20.506408, 20.508618, 20.510823, 20.513024, 20.515219, 20.517410, 20.519595, 20.521776, 20.523953, 20.526124, 20.528291, 20.530453, 20.532611, 20.534764, 20.536912, 20.539055, 20.541195, 20.543329, 20.545459, 20.547584, 20.549705, 20.551822, 20.553934, 20.556041, 20.558144, 20.560243, 20.562337, 20.564427, 20.566512, 20.568593, 20.570670, 20.572743, 20.574811, 20.576875, 20.578935, 20.580990, 20.583042, 20.585089, 20.587132, 20.589170, 20.591205, 20.593235, 20.595262, 20.597284, 20.599302, 20.601316, 20.603326, 20.605332, 20.607334, 20.609332, 20.611326, 20.613316, 20.615302, 20.617285, 20.619263, 20.621237, 20.623208, 20.625174, 20.627137, 20.629096, 20.631051, 20.633002, 20.634949, 20.636893, 20.638833, 20.640769, 20.642701, 20.644630, 20.646555, 20.648476, 20.650394, 20.652308, 20.654218, 20.656124, 20.658027, 20.659927, 20.661823, 20.663715, 20.665603, 20.667488, 20.669370, 20.671248, 20.673122, 20.674993, 20.676860, 20.678724, 20.680585, 20.682442, 20.684295, 20.686146, 20.687992, 20.689836, 20.691675, 20.693512, 20.695345, 20.697175, 20.699002, 20.700825, 20.702645, 20.704461, 20.706274, 20.708084, 20.709891, 20.711694, 20.713495, 20.715291, 20.717085, 20.718876, 20.720663, 20.722447, 20.724228, 20.726006, 20.727780, 20.729552, 20.731320, 20.733086, 20.734848, 20.736607, 20.738363, 20.740115, 20.741865, 20.743612, 20.745356, 20.747096, 20.748834, 20.750568, 20.752300, 20.754029, 20.755754, 20.757477, 20.759197, 20.760913, 20.762627, 20.764338, 20.766046, 20.767751, 20.769453, 20.771152, 20.772849, 20.774542, 20.776233, 20.777921, 20.779606, 20.781288, 20.782967, 20.784643, 20.786317, 20.787988, 20.789656, 20.791321, 20.792984, 20.794643, 20.796300, 20.797955, 20.799606, 20.801255, 20.802901, 20.804544, 20.806185, 20.807823, 20.809459, 20.811091, 20.812721, 20.814348, 20.815973, 20.817595, 20.819215, 20.820831, 20.822446, 20.824057, 20.825666, 20.827273, 20.828877, 20.830478, 20.832077, 20.833673, 20.835266, 20.836857, 20.838446, 20.840032, 20.841616, 20.843197, 20.844775, 20.846351, 20.847925, 20.849496, 20.851065, 20.852631, 20.854194, 20.855756, 20.857315, 20.858871, 20.860425, 20.861977, 20.863526, 20.865073, 20.866617, 20.868159, 20.869699, 20.871236, 20.872771, 20.874303, 20.875834, 20.877361, 20.878887, 20.880410, 20.881931, 20.883450, 20.884966, 20.886480, 20.887992, 20.889501, 20.891008, 20.892513, 20.894016, 20.895516, 20.897014, 20.898510, 20.900004, 20.901495, 20.902985, 20.904472, 20.905956, 20.907439, 20.908919, 20.910397, 20.911874, 20.913347, 20.914819, 20.916289, 20.917756, 20.919221, 20.920684, 20.922145, 20.923604, 20.925061, 20.926515, 20.927967, 20.929418, 20.930866, 20.932312, 20.933756, 20.935198, 20.936638, 20.938076, 20.939512, 20.940945, 20.942377, 20.943807, 20.945234, 20.946660, 20.948083, 20.949505, 20.950924, 20.952341, 20.953757, 20.955170, 20.956582, 20.957991, 20.959399, 20.960804, 20.962208, 20.963609, 20.965009, 20.966406, 20.967802, 20.969196, 20.970588, 20.971977, 20.973365, 20.974751, 20.976135, 20.977518, 20.978898, 20.980276, 20.981653, 20.983027, 20.984400, 20.985771, 20.987140, 20.988507, 20.989872, 20.991235, 20.992597, 20.993956, 20.995314, 20.996670, 20.998024, 20.999376, 21.000727, 21.002075, 21.003422, 21.004767, 21.006110, 21.007452, 21.008791, 21.010129, 21.011465, 21.012799, 21.014132, 21.015463, 21.016791, 21.018119, 21.019444, 21.020768, 21.022089, 21.023410, 21.024728, 21.026045, 21.027360, 21.028673, 21.029984, 21.031294, 21.032602, 21.033908, 21.035213, 21.036516, 21.037817, 21.039117, 21.040415, 21.041711, 21.043005, 21.044298, 21.045589, 21.046879, 21.048167, 21.049453, 21.050737, 21.052020, 21.053301, 21.054581, 21.055859, 21.057135, 21.058410, 21.059683, 21.060954, 21.062224, 21.063493, 21.064759, 21.066024, 21.067288, 21.068549, 21.069810, 21.071068, 21.072325, 21.073581, 21.074835, 21.076087, 21.077338, 21.078587, 21.079835, 21.081081, 21.082325, 21.083569, 21.084810, 21.086050, 21.087288, 21.088525, 21.089760, 21.090994, 21.092227, 21.093457, 21.094687, 21.095914, 21.097141, 21.098365, 21.099589, 21.100810, 21.102031, 21.103249, 21.104467, 21.105682, 21.106897, 21.108110, 21.109321, 21.110531, 21.111739, 21.112946, 21.114152, 21.115356, 21.116559, 21.117760, 21.118960, 21.120158, 21.121355, 21.122550, 21.123744, 21.124937, 21.126128, 21.127318, 21.128506, 21.129693, 21.130879, 21.132063, 21.133246, 21.134427, 21.135607, 21.136785, 21.137963, 21.139138, 21.140313, 21.141486, 21.142657, 21.143828, 21.144997, 21.146164, 21.147330, 21.148495, 21.149659, 21.150821, 21.151981, 21.153141, 21.154299, 21.155456, 21.156611, 21.157765, 21.158918, 21.160069, 21.161219, 21.162368, 21.163516, 21.164662, 21.165807, 21.166950, 21.168092, 21.169233, 21.170373, 21.171511, 21.172648, 21.173784, 21.174918, 21.176051, 21.177183, 21.178314, 21.179443, 21.180571, 21.181698, 21.182823, 21.183948, 21.185071, 21.186192, 21.187313, 21.188432, 21.189550, 21.190667, 21.191782, 21.192896, 21.194009, 21.195121, 21.196231, 21.197341, 21.198449, 21.199556, 21.200661, 21.201766, 21.202869, 21.203971, 21.205071, 21.206171, 21.207269, 21.208366, 21.209462, 21.210557, 21.211650, 21.212743, 21.213834, 21.214924, 21.216012, 21.217100, 21.218186, 21.219271, 21.220355, 21.221438, 21.222520, 21.223600, 21.224680, 21.225758, 21.226835, 21.227911, 21.228986, 21.230059, 21.231131, 21.232203, 21.233273, 21.234342, 21.235410, 21.236476, 21.237542, 21.238606, 21.239669, 21.240731, 21.241793, 21.242852, 21.243911, 21.244969, 21.246025, 21.247081, 21.248135, 21.249188, 21.250240, 21.251291, 21.252341, 21.253390, 21.254438, 21.255484, 21.256530, 21.257574, 21.258617, 21.259660, 21.260701, 21.261741, 21.262780, 21.263818, 21.264854, 21.265890, 21.266925, 21.267958, 21.268991, 21.270022, 21.271053, 21.272082, 21.273110, 21.274138, 21.275164, 21.276189, 21.277213, 21.278236, 21.279258, 21.280279, 21.281299, 21.282318, 21.283335, 21.284352, 21.285368, 21.286383, 21.287396, 21.288409, 21.289421, 21.290431, 21.291441, 21.292449, 21.293457, 21.294463, 21.295469, 21.296473, 21.297477, 21.298479, 21.299481, 21.300482}; 2 int main() 3 { 4 // long double ans=0; 5 // for(int i=1;i<=1000000000;i++) 6 // { 7 // ans+=1.0/i; 8 // if(i%1000000==0) 9 // printf("%Lf,", ans); 10 // } 11 int n; 12 while(~scanf("%d", &n)) 13 { 14 int p=n/1000000; 15 long double ans=biao[p]; 16 for(int i=p*1000000+1;i<=n;i++) 17 ans+=1.0/i; 18 printf("%.4Lf\n", ans); 19 } 20 return 0; 21 }
2.另一种是官方题解的方法
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 //#include <bits/stdc++.h> 2 #include <cstdio> 3 #include <string> 4 #include <cmath> 5 #include <cstring> 6 #include <iostream> 7 #include <sstream> 8 #include <algorithm> 9 using namespace std; 10 typedef long long LL; 11 const double eps=1e-2; 12 const int mod=10056; 13 const int N=1e6+5; 14 #define lson l, m, rt<<1 15 #define rson m+1, r, rt<<1|1 16 const double E=2.718281828459; 17 18 double a[2000001]; 19 int main() 20 { 21 double sum=0.0; 22 for(int i=1; i<=2000001; i++) 23 { 24 sum=sum+1.0/i; 25 a[i]=sum; 26 } 27 int n; 28 while(~scanf("%d",&n)) 29 if(n<=2000001) 30 printf("%.4lf\n",a[n]); 31 else 32 printf("%.4lf\n",log(double(n+1))/log(E)+0.577218); 33 return 0; 34 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 //#include <bits/stdc++.h> 2 #include <cstdio> 3 #include <string> 4 #include <cmath> 5 #include <cstring> 6 #include <iostream> 7 #include <sstream> 8 #include <algorithm> 9 using namespace std; 10 #include <vector> 11 typedef long long LL; 12 const double eps=1e-2; 13 const int mod=10056; 14 const int N=1e6+5; 15 #define lson l, m, rt<<1 16 #define rson m+1, r, rt<<1|1 17 const double E=2.718281828459; 18 int a[500]; 19 bool vis[500]; 20 double comb[500][500]; 21 double dp[500][500]; 22 23 int main() 24 { 25 for(int i=0;i<500;i++) 26 for(int j=0;j<=i;j++) 27 comb[i][j]=((i==0 || j==0)? 1:(comb[i-1][j]+comb[i-1][j-1])); 28 int t; 29 scanf("%d", &t); 30 while(t--) 31 { 32 int n, k; 33 scanf("%d%d", &n, &k); 34 vector<int> cyc; 35 for(int i=0;i<n;i++) 36 { 37 scanf("%d", &a[i]); 38 a[i]--; 39 } 40 memset(vis, 0, sizeof(vis)); 41 for(int i=0;i<n;i++) 42 { 43 if(vis[i]) 44 continue; 45 int x=i; 46 int cur=0; 47 while(!vis[x]) 48 { 49 cur++; 50 vis[x]=true; 51 x=a[x]; 52 } 53 cyc.push_back(cur); 54 } 55 for(int i=0;i<500;i++) 56 for(int j=0;j<500;j++) 57 dp[i][j]=0; 58 dp[0][0]=1; 59 for(int i=0;i<cyc.size();i++) 60 { 61 int x=cyc[i]; 62 for(int j=0;j<=k;j++) 63 for (int l=1;l+j<=k && l<=x; l++) 64 dp[i+1][j+l]+=dp[i][j]*comb[x][l]; 65 } 66 double ans=dp[cyc.size()][k]/comb[n][k]; 67 printf("%0.4lf\n", ans); 68 } 69 return 0; 70 }