A
描述
思路
令
f
[
i
]
[
j
]
f[i][j]
f[i][j]为放
i
i
i个
A
A
A,
j
j
j个
B
B
B的合法方案数。
存在以下划分方式:
1.
1.
1.
i
<
=
n
i<=n
i<=n时,i可以随便放
2.
2.
2.
j
<
=
m
j<=m
j<=m时,j可以随便放
3.
3.
3.
i
>
n
i>n
i>n时,由于
A
B
AB
AB的数量要小于等于
n
n
n,则
i
−
j
i-j
i−j为至少放了多少个
A
B
AB
AB
4.
4.
4.
j
>
m
j>m
j>m时,由于
B
A
BA
BA的数量要小于等于
m
m
m,则
j
−
i
j-i
j−i为至少放了多少个
B
A
BA
BA
我们对第一种和第二种情况无需处理,只需要处理第三和第四种情况即可,得到转移方程
i
f
(
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])
i
f
(
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
,
3
1,2,1,3
1,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]中出现,且出现了
4
4
4次,贡献为
1
∗
4
1*4
1∗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]中出现,且出现了
6
6
6次。发现,
a
[
2
]
a[2]
a[2]在后面的
n
−
i
+
1
n-i+1
n−i+1个区间中都出现,如果再加上
2
2
2前面的
a
[
1
]
a[1]
a[1],就又有了
n
−
i
+
1
n-i+1
n−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]中出现,且出现了
6
6
6次。但是
a
[
3
]
a[3]
a[3]和
a
[
1
]
a[1]
a[1]是相同的,实际贡献为
2
∗
3
−
2
∗
1
=
4
2*3-2*1=4
2∗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 ∗ b x*b x∗b和 a ∗ y a*y a∗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;
}