10.6 模拟赛の终章【题解*4】
作者:互联网
50 + 100 + 100 + 60 = 310 p t s 50+100+100+60=310pts 50+100+100+60=310pts
感觉这次模拟赛的题目质量很高
T 1 : P 2679 [ N O I P 2015 提 高 组 ] 子 串 {\color{green}{T1:P2679 [NOIP2015 提高组] 子串}} T1:P2679[NOIP2015提高组]子串
思路:
不知道是不是故意选的?感觉和昨天的 T 3 T3 T3很像,都是 d p dp dp+滚动数组优化。这道题目是想得最久的本来靠打暴力过了前五个点得到了 50 p t s 50pts 50pts,后来看题解想出了优化方法。
期望得分: 50 p t s . 50pts. 50pts.
实际得分 : 50 p t s . 50pts. 50pts.
反思 : 没有A这道题是很不应该的, P r i m o P a n PrimoPan PrimoPan老师昨天刚讲过 q w q qwq qwq.
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int n,m,k,now,ans[2][202][202],sum[2][202][202];
char a[1010],b[202];
int main(){
cin>>n>>m>>k;
scanf("%s%s",a+1,b+1);
ans[0][0][0]=ans[1][0][0]=now=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
for(int l=1;l<=k;l++){
if(a[i]==b[j])sum[now][j][l]=(sum[now^1][j-1][l]+ans[now^1][j-1][l-1])%mod;
else sum[now][j][l]=0;
ans[now][j][l]=(ans[now^1][j][l]+sum[now][j][l])%mod;
}
}
now^=1;
}
printf("%d\n",ans[now^1][m][k]);
}//tourist,bless me!
T 2 : P 2341 [ U S A C O 03 F A L L ] [ H A O I 2006 ] 受 欢 迎 的 牛 G {\color{green}{T2:P2341 [USACO03FALL][HAOI2006]受欢迎的牛 G}} T2:P2341[USACO03FALL][HAOI2006]受欢迎的牛G
省选题,然而难度难度并不是省选题难度。盲猜是
B
e
n
q
\pink{Benq}
Benq出的。
期望得分: 100 p t s . 100pts. 100pts.
实际得分 : 100 p t s . 100pts. 100pts.
思路:
这题和我之前做过的一道洛谷月赛题目是差不多的。首先是个人都能看出来要用
t
a
r
j
a
n
tarjan
tarjan缩点。因为既然奶牛之间的爱慕关系具有传递性,那么在同一个强联通分量里的奶牛一定是互相爱慕的。缩完所有的
S
C
C
SCC
SCC后的图显然就是一个
D
A
G
DAG
DAG了。这时候图可能有一个节点,也可能有很多节点。若只有一个节点,那么显然的这里面所有的奶牛都是明星。如果有很多节点,我们就要分情况讨论。先
O
(
N
M
)
O(NM)
O(NM)地遍历一遍统计各个结点(缩完点之后的)的出度,并记录出度为
0
0
0的结点数量,记为
s
u
m
sum
sum。如果
s
u
m
sum
sum=1。那么显然这个
D
A
G
DAG
DAG是一条链,最后一个结点是所有节点的后继,显然这个点里面的奶牛都是明星。如果
sum
≥
2
\operatorname{sum}\geq2
sum≥2,那么这个
D
A
G
DAG
DAG显然有多个分支,那么这些末端的点的奶牛是无法相互爱慕的,所以这时没有任何奶牛是明星。
附上代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4;
int n,m,cnt,id,top,ans;
int v[maxn],dfn[maxn],low[maxn],st[maxn],color[maxn],num[maxn],deg[maxn];
vector<int> ver[maxn];
inline void read()
{
cin>>n>>m;
for(int i=1;i<=m;i++)
{
int u,v;
cin>>u>>v;
ver[u].push_back(v);
}
}
inline void tarjan(int x)
{
v[x]=1;
low[x]=dfn[x]=++id;
st[++top]=x;
int ts;
for(int y=0;y<ver[x].size();y++)
{
int t=ver[x][y];
if(!dfn[t])
{
tarjan(t);
low[x]=min(low[x],low[t]);
}
else if(v[t])
{
low[x]=min(low[x],dfn[t]);
}
}
if(low[x]==dfn[x])
{
cnt++;
do
{
ts=st[top--];
color[ts]=cnt;
num[cnt]++;
v[ts]=0;
}while(x!=ts);
}
}
int main()
{
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout);
ios::sync_with_stdio(false);
read();
for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);
for(int i=1;i<=n;i++)
{
for(int j=0;j<ver[i].size();j++)
{
int t=ver[i][j];
if(color[i]!=color[t]) deg[color[i]]++;
}
}
for(int i=1;i<=cnt;i++)
{
if(!deg[i])
{
if(ans)
{
cout<<0<<endl;
return 0;
}
ans=i;
}
}
cout<<num[ans]<<endl;
return 0;
} // tourist,bless me!
T 3 : P 1092 [ N O I P 2004 提 高 组 ] 虫 食 算 {\color{Blue}{T3 : P1092 [NOIP2004 提高组] 虫食算}} T3:P1092[NOIP2004提高组]虫食算
明显的 G a u s s \mathtt {Gauss} Gauss消元。
期望得分: 100 p t s . 100pts. 100pts.
实际得分 : 100 p t s . 100pts. 100pts.
解题思路:
由于是加法,所以不考虑进位不为
1
1
1的情况。我们用
d
i
d_i
di表示每一位因上一位加法而获得的进位贡献(综上所述只能为
0
0
0或
1
1
1),设第一个数各个位的数字为
a
i
a_i
ai,第二个数为
b
i
b_i
bi,结果为
c
i
c_i
ci,对于每一位所以我们得到了一个线性方程:
(
a
i
+
b
i
+
d
i
−
1
)
mod
n
=
c
i
(a_i+b_i+d_{i-1})\operatorname{mod}\space n=c_i
(ai+bi+di−1)mod n=ci
消去取模符号,得:
a
i
+
b
i
+
d
i
−
1
=
c
i
+
k
×
n
a_i+b_i+d_{i-1}=c_i+k\times n
ai+bi+di−1=ci+k×n
因为这是竖式运算,所以上面的式子可以转化为:
a
i
+
b
i
+
d
i
−
1
=
c
i
+
n
×
d
i
.
(
i
∈
[
1
,
n
]
,
i
∈
N
)
a_i+b_i+d_{i-1}=c_i+n\times d_i.(i\in[1,n],i\in\mathbb N)
ai+bi+di−1=ci+n×di.(i∈[1,n],i∈N)
这是一组线性方程,依据题意建立增广矩阵,枚举
d
i
d_i
di求解即可。复杂度为
O
(
2
n
−
1
n
3
)
O(2^{n-1}n^3)
O(2n−1n3),
n
n
n最大为
26
26
26,应该可以过?
A C C o d e : AC\space Code: AC Code:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 26
int n,equ,var,d[MAXN+10],x[MAXN+10];
typedef int matrix[MAXN+10][MAXN+10];
matrix a,g;
bool vis[MAXN+10];
char s[3][MAXN+10];
void Read(int &x){
char c;
while(c=getchar(),c!=EOF)
if(c>='0'&&c<='9'){
x=c-'0';
while(c=getchar(),c>='0')
x=x*10+c-'0';
ungetc(c,stdin);
return;
}
}
void read(){
Read(n);
scanf("%s%s%s",s[0],s[1],s[2]);
int i,j;
for(i=0;i<n;i++){
for(j=0;j<2;j++)
a[n-i][s[j][i]-'A'+1]++;
a[n-i][s[2][i]-'A'+1]--;
}
for(i=1;i<=n;i++)
g[i][i]=n,g[i][i-1]=-1;
g[1][0]=0;
equ=var=n;
}
int gcd(int a,int b){
int t;
while(b){
t=a%b;
a=b;
b=t;
}
return a;
}
void gauss_jordan(){
int i,j,row,col,mxr,lcm;
for(row=col=1;row<=equ&&col<=var;row++,col++){
mxr=row;
for(i=row+1;i<=equ;i++)
if(abs(a[i][col])>abs(a[mxr][col]))
mxr=i;
if(mxr!=row)
swap(a[row],a[mxr]),swap(g[row],g[mxr]);
if(!a[row][col]){
row--;
continue;
}
for(i=1;i<=equ;i++)
if(i!=row&&a[i][col]){
lcm=a[i][col]/gcd(a[i][col],a[row][col])*a[row][col];
int t1=lcm/a[i][col],t2=lcm/a[row][col];
for(j=1;j<=var;j++){
g[i][j]=t1*g[i][j]-t2*g[row][j];
a[i][j]=t1*a[i][j]-t2*a[row][j];
}
}
}
}
bool check(){
int i,j;
memset(vis,0,sizeof vis);
for(i=1;i<=n;i++){
x[i]=0;
for(j=1;j<=n;j++)
x[i]+=g[i][j]*d[j];
if(x[i]%a[i][i]||x[i]/a[i][i]<0||x[i]/a[i][i]>=n||vis[x[i]/a[i][i]])
return 0;
x[i]/=a[i][i];
vis[x[i]]=1;
}
return 1;
}
void print(){
for(int i=1;i<n;i++)
printf("%d ",x[i]);
printf("%d\n",x[n]);
}
void dfs(int i){
if(i==n){
if(check()){
print();
exit(0);
}
return;
}
d[i]=1;
dfs(i+1);
d[i]=0;
dfs(i+1);
}
int main()
{
read();
gauss_jordan();
dfs(1);
}
T 4 : [ H A O I 2012 ] 外 星 人 {\color{purple}{T4:[HAOI2012]外星人}} T4:[HAOI2012]外星人
期望得分: 30 p t s . 30pts. 30pts.
实际得分 : 60 p t s . 因 为 吸 了 氧 q w q 60pts.\color{red}{因为吸了氧qwq} 60pts.因为吸了氧qwq
解题思路:
没什么思路,题意很明了。这是一道名副其实的省选题,所以我压根就没想拿满分。直接套 O ( n ) O(\sqrt{n}) O(n )的欧拉函数,再一层循环了事。总复杂度为 O ( n n ) O(n\sqrt{n}) O(nn ),开了 LongLong \operatorname{LongLong} LongLong,吸氧 60 p t s 60pts 60pts到手。
C o d e : \red{Code:} Code:
#include<bits/stdc++.h>
using namespace std;
inline long long phi(long long n)
{
long long ans=n;
for(int i=2;i<=sqrt(n);i++)
{
if(n%i==0)
{
ans=ans/i*(i-1);
while(n%i==0) n/=i;
}
}
if(n>1) ans=ans/n*(n-1);
return ans;
}
int main()
{
int test;
cin>>test;
while(test--)
{
int m,p,q;
cin>>m;
long long n=1;
for(int i=1;i<=m;i++)
{
cin>>p>>q;
n*=pow(p,q);
}
int ans=0;
while(n>1)
{
n=phi(n);
ans++;
}
cout<<ans<<endl;
}
return 0;
}
写 在 最 后 : 写在最后: 写在最后:
要 注 意 提 高 思 维 能 力 , 把 板 子 打 熟 。 \color{Blue}{要注意提高思维能力,把板子打熟。} 要注意提高思维能力,把板子打熟。
标签:10.6,int,题解,sum,color,ans,终章,now,row 来源: https://blog.csdn.net/qq_62444770/article/details/120622082