A、B、C三道题题解
一.A题
(1)题意:
t组测试数据,每组测试数据给出一个n,将1~n这些数排列组合,当ai | ai+1 | …… | aj >= j - i + 1,
即区间[ i , j ]的或运算 >= j - i + 1。
(2)题解:
根据样例和或运算的性质(只要有一个为1就为1),所以其实直接按照顺序输出就好了。
(3)代码:
#include <stdio.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<functional>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 10;
const int INF = 0x3f3f3f3f;
int t,n;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i = 1; i < n; i++)
printf("%d ",i);
printf("%d\n",n);
}
return 0;
}
二.B题
(1)题意:
t组测试数据,每组测试数据有n * m个格子大小,然后每个格子上有一个字母R(可以向右一步)或者D(可以向下一步),问最少改变多少个格子上面的字母,使每一个格子都能到(n,m)这个格子
(2)题解:
因为只能向下或者向右,所以所有的格子肯定都能到最后一列或者最后一行,所以只需要修改最后一列和最后一行即可,也就是把第n行上面是D -> R、第m列上面是R - > D。
(3)代码:
#include <stdio.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<functional>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 10;
const int INF = 0x3f3f3f3f;
int t,n,m,ans;
char mp[maxn][maxn];
int main()
{
scanf("%d",&t);
while(t--)
{
ans = 0;
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
scanf(" %c",&mp[i][j]);
if(i == n && mp[i][j] != 'R' && j != m)
ans++;
else if(j == m && mp[i][j] != 'D' && i != n)
ans++;
}
printf("%d\n",ans);
}
return 0;
}
三.C题
(1)题意:
给一个数字n,问1~n的全排列中能够组成多少个具有循环的排列
每一个数ai能够和序列位置上最靠近他左右两边各一个数aj ( j < i ), ak ( k > i )且这个数大于当前选择的ai,即(aj > ai)( ak > ai )
j 到 i就能够连成一条无向边,i 到 k 也能够连成一条边。
最后的结果对1e9 + 7 取模。
(2)题解:
首先枚举出来样例中4的全排列,发现只有当出现p1 < p2 < …… < pk > …… > pn,即出现了单峰排列的情况,就不会出现循环。
也就是用 n的全排列 - 单峰排列出现的次数,n! - 2n-1。
(3)代码:
#include <stdio.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<functional>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 10;
const int INF = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
ll n,ans;
ll mod_pow(ll x, ll m)
{
ll res = 1;
while(m > 0){
if(m & 1) res = res * x % mod;
x = x * x % mod;
m >>= 1;
}
return res;
}
int main()
{
scanf("%lld",&n);
ans = 1;
for(int i = 1; i <= n; i++)
ans = (ans * i) % mod;
printf("%lld",((ans - mod_pow(2,n-1)) % mod + mod) % mod);
return 0;
}