CF611F. New Year and Cleaning
Solution
还挺巧妙的套路。
把起点整体看作一个矩阵,在操作时移出原来矩阵外的部分的起点都是超越边界的,可以直接通过超出的面积计算贡献,再把超出的部分删去,模拟即可(第一轮模拟会与之后有所不同)。
Code
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second
#define int llusing namespace std;template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=1e9+7;
const int MAXN=1000005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{int f=1,x=0; char c=getchar();while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }return x*f;
}
char st[MAXN];
PR c[MAXN];
int upd(int x,int y) { return x+y>=mods?x+y-mods:x+y; }
void get(int &x,int &y,char ch)
{if (ch=='R') y++;if (ch=='L') y--;if (ch=='D') x--;if (ch=='U') x++;
}
signed main()
{int n=read(),H=read(),W=read(),x2=H,y2=W,x1=1,y1=1,ans=0;scanf("%s",st+1);for (int i=1;i<=n;i++){get(x1,y1,st[i]);get(x2,y2,st[i]);if (x1==0&&x1<=x2&&y1<=y2) x1=1,ans=upd(ans,(y2-y1+1)*i%mods);if (y1==0&&x1<=x2&&y1<=y2) y1=1,ans=upd(ans,(x2-x1+1)*i%mods);if (x2==H+1&&x1<=x2&&y1<=y2) x2=H,ans=upd(ans,(y2-y1+1)*i%mods);if (y2==W+1&&x1<=x2&&y1<=y2) y2=W,ans=upd(ans,(x2-x1+1)*i%mods);}int num=0;for (int i=1;i<=n;i++){get(x1,y1,st[i]);get(x2,y2,st[i]);if (x1==0&&x1<=x2&&y1<=y2) x1=1,ans=upd(ans,(y2-y1+1)*(i+n)%mods),c[++num]=MP(i,0);if (y1==0&&x1<=x2&&y1<=y2) y1=1,ans=upd(ans,(x2-x1+1)*(i+n)%mods),c[++num]=MP(i,1);if (x2==H+1&&x1<=x2&&y1<=y2) x2=H,ans=upd(ans,(y2-y1+1)*(i+n)%mods),c[++num]=MP(i,0);if (y2==W+1&&x1<=x2&&y1<=y2) y2=W,ans=upd(ans,(x2-x1+1)*(i+n)%mods),c[++num]=MP(i,1);}if (!num&&x1<=x2&&y1<=y2) { puts("-1"); return 0; }for (int nw=2,x=x2-x1+1,y=y2-y1+1;x>0&&y>0;nw++)for (int i=1;i<=num;i++) {if (c[i].se==0&&x>0&&y>0) ans=upd(ans,max(y,0ll)*(nw*n%mods+c[i].fi)%mods),x--;if (c[i].se==1&&x>0&&y>0) ans=upd(ans,max(x,0ll)*(nw*n%mods+c[i].fi)%mods),y--;}printf("%lld\n",ans);return 0;
}