A
描述
思路
令f[i][j]f[i][j]f[i][j]为放iii个AAA,jjj个BBB的合法方案数。
存在以下划分方式:
1.1.1. i<=ni<=ni<=n时,i可以随便放
2.2.2. j<=mj<=mj<=m时,j可以随便放
3.3.3. i>ni>ni>n时,由于ABABAB的数量要小于等于nnn,则i−ji-ji−j为至少放了多少个ABABAB
4.4.4. j>mj>mj>m时,由于BABABA的数量要小于等于mmm,则j−ij-ij−i为至少放了多少个BABABA
我们对第一种和第二种情况无需处理,只需要处理第三和第四种情况即可,得到转移方程
if(i−j>=n)if(i-j>=n)if(i−j>=n) f[i][j]=(f[i][j]+f[i−1][j])f[i][j]=(f[i][j]+f[i-1][j])f[i][j]=(f[i][j]+f[i−1][j])
if(j−i>=m)if(j-i>=m)if(j−i>=m) f[i][j]=(f[i][j]+f[i][j−1])f[i][j]=(f[i][j]+f[i][j-1])f[i][j]=(f[i][j]+f[i][j−1])
最后的f[n][m]f[n][m]f[n][m]即为我们的答案。
AC代码
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <set>
#include <stack>
#include <iomanip>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define sz(x) x.size()
#define IOS ios::sync_with_stdio(false);cin.tie(0);
#define seteps(N) setprecision(N)
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<char,char> PCC;
typedef long long ll;
typedef pair<ll,ll> PLL;
typedef __int128 lll;
const int N=2*1010;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const lll one=1;
ll f[N][N];
void solve(){
double p=3.1415926;
int n,m;
while(cin>>n>>m){
rep(i,0,n+m+1){
rep(j,0,n+m+1){
f[i][j]=0;
}
}
f[0][0]=1;
rep(i,0,n+m+1){
rep(j,0,n+m+1){
if(i>=1 && i-j<=n) f[i][j]=(f[i][j]+f[i-1][j])%mod;
if(j>=1 && j-i<=m) f[i][j]=(f[i][j]+f[i][j-1])%mod;
}
}
cout<<f[n+m][n+m]<<endl;
}
}
int main(){
IOS;
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
//int t;cin>>t;
//while(t--)
solve();
return 0;
}
B
题意描述
思路
对于每个数字,我们求出它的贡献。
以1,2,1,31,2,1,31,2,1,3为例。
a[1]a[1]a[1]在[1,1],[1,2],[1,3],[1,4][1,1],[1,2],[1,3],[1,4][1,1],[1,2],[1,3],[1,4]中出现,且出现了444次,贡献为1∗41*41∗4;
a[2]a[2]a[2]在[2,2],[2,3],[2,4],[1,2],[1,3],[1,4][2,2],[2,3],[2,4],[1,2],[1,3],[1,4][2,2],[2,3],[2,4],[1,2],[1,3],[1,4]中出现,且出现了666次。发现,a[2]a[2]a[2]在后面的n−i+1n-i+1n−i+1个区间中都出现,如果再加上222前面的a[1]a[1]a[1],就又有了n−i+1n-i+1n−i+1个贡献。
a[3]a[3]a[3]在[3,3],[3,4],[1,3],[1,4],[2,3],[2,4][3,3],[3,4],[1,3],[1,4],[2,3],[2,4][3,3],[3,4],[1,3],[1,4],[2,3],[2,4]中出现,且出现了666次。但是a[3]a[3]a[3]和a[1]a[1]a[1]是相同的,实际贡献为2∗3−2∗1=42*3-2*1=42∗3−2∗1=4。
a[4]a[4]a[4]同理。
我们发现,如果该数字是第一次出现,则该数字的贡献为左区间数∗*∗右区间数。否则数字贡献为当前下标和第一次出现该数字下标之间的位置∗*∗右区间数。
AC代码
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <set>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define sz(x) x.size()
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<char,char> PCC;
typedef long long ll;
typedef pair<ll,ll> PLL;
typedef __int128 lll;
const int N=2*1e5+10;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const lll one=1;
int a[N];
void solve(){
int n;
cin>>n;
rep(i,1,n+1) cin>>a[i];
ll ans=0;
map<int,int> used;
rep(i,1,n+1){
if(!used[a[i]]){
ans+=(i*(n-i+1));
}else{
ans+=(n-i+1)*(i-used[a[i]]);
}
used[a[i]]=i;
}
cout<<ans<<endl;
}
int main(){
IOS;
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
//int t;cin>>t;
//while(t--)
solve();
return 0;
}
F
题意描述
思路
判断x∗bx*bx∗b和a∗ya*ya∗y的大小即可。由于数据范围太大,可以使用JAVA的大整数和py或者C++的__int128来解决。
AC代码
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <set>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define sz(x) x.size()
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<char,char> PCC;
typedef long long ll;
typedef pair<ll,ll> PLL;
typedef __int128 lll;
const int N=2*1e5+10;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const lll one=1;
int a[N];
void solve(){
ll a,b,x,y;
while(cin>>x>>a>>y>>b){
lll res1=one*x*b;
lll res2=one*a*y;
if(res1==res2) cout<<"="<<endl;
else if(res1<res2) cout<<"<"<<endl;
else cout<<">"<<endl;
}
}
int main(){
IOS;
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
//int t;cin>>t;
//while(t--)
solve();
return 0;
}
G
题意描述
思路
使用一个字符串来储存删除过后的字符串序列,使用一个变量来表示删除后的字符串下标。每次符合条件时,变量都要向前移3位,模拟这个过程即可。
AC代码
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <set>
#include <stack>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define sz(x) x.size()
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<char,char> PCC;
typedef long long ll;
typedef pair<ll,ll> PLL;
typedef __int128 lll;
const int N=2*1e5+10;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const lll one=1;
void solve(){
string s,v;
cin>>s;
v=s;
int now=0,ans=0;
rep(i,0,sz(s)){
now++;
v[now]=s[i];
if(now>=3 && v[now]==v[now-1] && v[now-2]==v[now]){
ans++;
now-=3;
}
}
cout<<ans<<endl;
}
int main(){
IOS;
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
//int t;cin>>t;
//while(t--)
solve();
return 0;
}