#include<bits/stdc++.h>usingnamespace std;// #define int long long#definedoublelongdoubleusing i64 =longlong;typedef pair<int,int> PII;typedef pair<int,char> PIC;typedef pair<double,double> PDD;typedef pair<PII,int> PIII;typedef pair<int, pair<int,bool>> PIIB;constint N =6e6+10;constint maxn =1e6;constint MAXN =100;constint mod =998244353;constint mod1 =954169327;constint mod2 =906097321;constint INF =0x3f3f3f3f3f3f3f3f;voidsolve(){int xx, yy; cin >> xx >> yy;
string s; cin >> s;bool lr =0, ud =0;if(xx ==0&& yy ==0){
cout <<"Impossible\n";return;}
vector<int>cnt(4);// UDLRint nowx =0, nowy =0;bool flag =false;for(auto t : s){if(t =='U'){
nowy ++;
cnt[0]++;}elseif(t =='D'){
nowy --;
cnt[1]++;}elseif(t =='L'){
nowx --;
cnt[2]++;}else{
nowx ++;
cnt[3]++;}if(nowx == xx && nowy == yy) flag =true;}if(cnt[0]&& cnt[1]) ud =true;if(cnt[2]&& cnt[3]) lr =true;if(!flag) cout << s <<'\n';else{if(nowx == xx && nowy == yy) cout <<"Impossible\n";else{
string ans ="";if(cnt[0]> cnt[1]){if(!(xx ==0&& yy ==1))for(int i =0; i < cnt[1]; i ++) ans +="UD";elsefor(int i =0; i < cnt[1]; i ++) ans +="DU";
cnt[0]-= cnt[1], cnt[1]=0;}else{if(!(xx ==0&& yy ==1))for(int i =0; i < cnt[0]; i ++) ans +="UD";elsefor(int i =0; i < cnt[0]; i ++) ans +="DU";
cnt[1]-= cnt[0], cnt[0]=0;}if(cnt[2]> cnt[3]){if(!(xx ==-1&& yy ==0))for(int i =0; i < cnt[3]; i ++) ans +="LR";elsefor(int i =0; i < cnt[3]; i ++) ans +="RL";
cnt[2]-= cnt[3], cnt[3]=0;}else{if(!(xx ==-1&& yy ==0))for(int i =0; i < cnt[2]; i ++) ans +="LR";elsefor(int i =0; i < cnt[2]; i ++) ans +="RL";
cnt[3]-= cnt[2], cnt[2]=0;}auto check1 =[&](){int x =0, y =0;for(int i =0; i < cnt[2]; i ++){
x --;if(xx == x && yy == y)returnfalse;}for(int i =0; i < cnt[3]; i ++){
x ++;if(xx == x && yy == y)returnfalse;}for(int i =0; i < cnt[0]; i ++){
y ++;if(xx == x && yy == y)returnfalse;}for(int i =0; i < cnt[1]; i ++){
y --;if(xx == x && yy == y)returnfalse;}returntrue;};auto check2 =[&](){int x =0, y =0;for(int i =0; i < cnt[0]; i ++){
y ++;if(xx == x && yy == y)returnfalse;}for(int i =0; i < cnt[1]; i ++){
y --;if(xx == x && yy == y)returnfalse;}for(int i =0; i < cnt[2]; i ++){
x --;if(xx == x && yy == y)returnfalse;}for(int i =0; i < cnt[3]; i ++){
x ++;if(xx == x && yy == y)returnfalse;}returntrue;};// 先左右后上下if(check1()){for(int i =0; i < cnt[2]; i ++) ans +='L';for(int i =0; i < cnt[3]; i ++) ans +='R';for(int i =0; i < cnt[0]; i ++) ans +='U';for(int i =0; i < cnt[1]; i ++) ans +='D';}elseif(check2())// 先上下后左右{for(int i =0; i < cnt[0]; i ++) ans +='U';for(int i =0; i < cnt[1]; i ++) ans +='D';for(int i =0; i < cnt[2]; i ++) ans +='L';for(int i =0; i < cnt[3]; i ++) ans +='R';}else{int tt =0;for(int i =0; i <4; i ++) tt +=(cnt[i]>0);if(tt !=1){
cout <<"Impossible\n";return;}else{if(cnt[0]>0|| cnt[1]>0){if(!lr){
cout <<"Impossible\n";return;}
ans.pop_back(), ans.pop_back();
ans +='L';for(int i =0; i < cnt[0]; i ++) ans +='U';for(int i =0; i < cnt[1]; i ++) ans +='D';
ans +='R';}else{if(!ud){
cout <<"Impossible\n";return;}for(int i =0; i +2< ans.size(); i ++) ans[i]= ans[i +2];
ans.pop_back(), ans.pop_back();
ans +='U';for(int i =0; i < cnt[2]; i ++) ans +='L';for(int i =0; i < cnt[3]; i ++) ans +='R';
ans +='D';}}}
cout << ans <<'\n';}}}signedmain(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);int t =1;
cin >> t;while(t--){solve();}return0;}
#include<bits/stdc++.h>usingnamespace std;#defineintlonglong#definemod1000000007int ans[100][100]={{0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,15,339,4761,52929,517761,4767849,43046721,387420489,486784380,381059392,429534507},{0,0,339,16485,518265,14321907,387406809,460338013,429534507,597431612,130653412,527642103,246336683},{0,0,4761,518265,43022385,486780060,429534507,792294829,175880701,246336683,953271190,214965851,412233812},{0,0,52929,14321907,486780060,288599194,130653412,748778899,953271190,644897553,710104287,555340537,947749553},{0,0,517761,387406809,429534507,130653412,246336683,579440654,412233812,518446848,947749553,909419307,966670169},{0,0,4767849,460338013,792294829,748778899,579440654,236701429,666021604,589237756,662963356,900849429,157687433},{0,0,43046721,429534507,175880701,953271190,412233812,666021604,767713261,966670169,322934415,772681989,566494346},{0,0,387420489,597431612,246336683,644897553,518446848,589237756,966670169,968803245,954137859,295347237,319625180},{0,0,486784380,130653412,953271190,710104287,947749553,662963356,322934415,954137859,886041711,876626606,924095353},{0,0,381059392,527642103,214965851,555340537,909419307,900849429,772681989,295347237,876626606,772286045,155055959},{0,0,429534507,246336683,412233812,947749553,966670169,157687433,566494346,319625180,924095353,155055959,93330098}};intksm(int a,int b){int res =1;while(b){if(b &1)
res = res * a % mod;
b >>=1;
a = a * a % mod;}return res;}signedmain(){int T;scanf("%lld",&T);while(T--){int n,m;scanf("%lld%lld",&n,&m);if(n<=10&&m<=10)printf("%lld\n",ans[n][m]);elseif(n ==1|| m ==1)printf("%lld\n",0ll);elseprintf("%lld\n",ksm(3,n*m));}return0;}
#include<bits/stdc++.h>usingnamespace std;// #define int long long#definedoublelongdoubleusing i64 =longlong;typedef pair<int,int> PII;typedef pair<int,char> PIC;typedef pair<double,double> PDD;typedef pair<PII,int> PIII;typedef pair<int, pair<int,bool>> PIIB;constint N =6e6+10;constint maxn =1e6;constint MAXN =100;constint mod =998244353;constint mod1 =954169327;constint mod2 =906097321;constint INF =0x3f3f3f3f3f3f3f3f;voidsolve(){int n, m; cin >> n >> m;
vector<PII>a(n + m +1);
set<int> st;for(int i =1; i <= n; i ++){
cin >> a[i].first;
a[i].second =0;}for(int i = n +1; i <= n + m; i ++){
cin >> a[i].first;
a[i].second =1;
st.insert(a[i].first);}sort(a.begin()+1, a.end());int ans =0, tmp =0;for(int i =1; i <= n + m; i ++){if(a[i].second ==0){if(st.count(a[i].first)==0) tmp ++;}else{
ans =max(ans, tmp);
tmp =0;}}
ans =max(ans, tmp);if(ans ==0) cout <<"Impossible\n";else cout << ans <<'\n';}signedmain(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);int t =1;
cin >> t;while(t--){solve();}return0;}
M - Monster Hunter(树形dp)
参考:2020 ICPC南京 M.Monster Hunter(树形背包)
#include<bits/stdc++.h>#include<bits/extc++.h>usingnamespace std;usingnamespace __gnu_pbds;#defineintlonglong#definedoublelongdoubleusing i64 =longlong;typedef pair<int,int> PII;typedef pair<int,char> PIC;typedef pair<double,double> PDD;typedef pair<PII,int> PIII;typedef pair<int, pair<int,bool>> PIIB;constint N =2e5+10;constint maxn =1e6;constint MAXN =100;constint mod =998244353;constint mod1 =954169327;constint mod2 =906097321;constint INF =0x3f3f3f3f3f3f3f3f;voidsolve(){int n; cin >> n;
vector<vector<int>>g(n +1);for(int i =2; i <= n; i ++){int x; cin >> x;
g[i].push_back(x);
g[x].push_back(i);}
vector<int>a(n +1),sz(n +1);for(int i =1; i <= n; i ++) cin >> a[i];
vector<vector<vector<int>>>dp(2,vector<vector<int>>(n +1,vector<int>(n +1, INF)));
function<void(int,int)> dfs =[&](int u,int fa){
dp[0][u][0]=0;
dp[1][u][1]= a[u];for(int i =0; i < g[u].size(); i ++){int j = g[u][i];if(j == fa)continue;dfs(j, u);}
sz[u]=1;for(int i =0; i < g[u].size(); i ++){int v = g[u][i];if(v == fa)continue;for(int j = sz[u]; j >=0; j --){for(int k = sz[v]; k >=0; k --){
dp[0][u][j + k]=min(dp[0][u][j + k], dp[0][u][j]+min(dp[0][v][k], dp[1][v][k]));
dp[1][u][j + k]=min(dp[1][u][j + k], dp[1][u][j]+min(dp[0][v][k], dp[1][v][k]+ a[v]));}}
sz[u]+= sz[v];}};dfs(1,-1);for(int i =0; i <= n; i ++){if(i !=0) cout <<' ';
cout <<min(dp[0][1][n - i], dp[1][1][n - i]);}
cout <<'\n';}signedmain(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);int t =1;
cin >> t;while(t--){solve();}return0;}