C
题意:给定字符串S,T,其中T是S插入一个字母得到的,问是在哪个位置插入的。
直接根据题意模拟即可。
code:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ios std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
#define x first
#define y second
const int N=4e5+10;
const double eps=1e-4;
typedef pair<int,int> pii;
void work()
{
string s,t;
cin>>s>>t;
int ans=s.size()+1;
for(int i=0;i<s.size();i++){
if(s[i]!=t[i]) {
ans=i+1; break;
}
}
cout<<ans<<endl;
}
signed main()
{
int t;
//cin>>t;
t=1;
while(t--)
{
work();
}
return 0;
}
D
题意:给定K,找到最小的N,使N!能够整除K。
K的范围是
1
≤
1\leq
1≤ K
≤
\leq
≤
1
0
12
10^{12}
1012,显然暴力做会超时。
对K进行质因数分解,注意到K的最大质因子不会超过
K
\sqrt{K}
K,
可能是数据较弱,我们可以暴力的循环,答案必定为1~
2
∗
1
0
6
2*10^6
2∗106,(注意这里范围改为
1
0
6
10^6
106就会wa三个点。)或者其本身。
暴力code:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ios std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
#define x first
#define y second
const int N=1e6+10;
const double eps=1e-4;
typedef pair<int,int> pii;
map<int,int> mp;
void work()
{
int k;
cin>>k;
for(int i=1;i<=3e6+10;i++){
k/=__gcd(k,i);
if(k==1) {
cout<<i<<endl;
return ;
}
}
cout<<k<<endl;
}
signed main()
{
int t;
//cin>>t;
t=1;
while(t--)
{
work();
}
return 0;
}
显然这不是正解,正解是把K分解质因子,如
a
1
p
1
{a_1}^{p_1}
a1p1,
a
2
p
2
{a_2}^{p_2}
a2p2,
a
3
p
3
{a_3}^{p_3}
a3p3 等等。
那怎么来求最小的N呢?
假设有分解后有
2
5
2^5
25,那么其实在N!中,2,4,6,8等等是都可以贡献2这个质因子的。因此我们需要计算出一个边界x,使得从2~x中的数,含有2质因子的总数要大于5。2含有1个2,4含有2个2,6含有1个2,8含有3个2,因此我们找到的边界就为8。
同理,找其他质因子的边界也是如此,答案即为所有边界的最大值。
比上述暴力快了几十倍。
code:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ios std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
#define x first
#define y second
const int N=1e6+10;
const double eps=1e-4;
typedef pair<int,int> pii;
int k;
int get(int i,int n)
{
int ans=0;
while(n%i==0) ans++,n/=i;
return ans;
}
int cal(int p,int x) //p^x
{
int cnt=0;
for(int i=1;;i++){
cnt+=get(p,p*i);
if(cnt>=x) return p*i;
}
}
void work()
{
cin>>k;
int ans=0;
for(int i=2;i<=k/i;i++){
if(k%i==0){
int cnt=0;
while(k%i==0) k/=i,cnt++;
ans=max(ans,cal(i,cnt));
}
}
if(k) ans=max(ans,k);
cout<<ans<<endl;
}
signed main()
{
int t;
//cin>>t;
t=1;
while(t--)
{
work();
}
return 0;
}
E
题意:打一个怪兽,有
p
100
\frac{p}{100}
100p 的概率失去2点血,有
100
−
p
100
\frac{100-p}{100}
100100−p 的概率失去1点血。
问把怪物杀死的期望次数为多少。
期望DP的模板题,没怎么做过这种题,学习一下。
code:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ios std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
#define x first
#define y second
const int N=1e6+10,mod=998244353;
const double eps=1e-4;
typedef pair<int,int> pii;
int dp[N];//dp[i]表示当前怪物有i的血量,要击败他的期望次数
int qmi(int a,int k)
{
int ans=1;
while(k)
{
if(k&1) ans=ans*a%mod;
a=a*a%mod;
k>>=1;
}
return ans;
}
void work()
{
//期望DP
//转移方程 dp[i]=(dp[i-1]+1)*(100-p)/100 + (dp[i-2]+1)*p/100
int n,p;
cin>>n>>p;
dp[0]=0; dp[1]=1;
for(int i=2;i<=n;i++){
dp[i]=(((dp[i-1]+1)*(100-p)%mod)*qmi(100,mod-2)%mod+
((dp[i-2]+1)*(p)%mod)*qmi(100,mod-2)%mod+mod)%mod;
}
cout<<dp[n]<<endl;
}
signed main()
{
int t;
//cin>>t;
t=1;
while(t--)
{
work();
}
return 0;
}