AtCoder Beginner Contest 215 (ABCD) 题解
(不想看/看不懂官方题解的人,可以看看下面我整理出来的题解。)
Q u e s t i o n A : \mathtt{Question~A:} Question A:
送分题,判断输入是否为Hello,World!
即可。
时间复杂度: O ( 1 ) O(1) O(1)
空间复杂度: O ( ∣ S ∣ ) O(|S|) O(∣S∣)
#include <iostream>
using namespace std;
string str;
int main()
{
cin >> str;
if(str=="Hello,World!")puts("AC");
else puts("WA");
return 0;
}
Q u e s t i o n B : \mathtt{Question~B:} Question B:
送分题,(不能直接用
l
o
g
\mathtt{log}
log),必须按题意模拟
k
\mathtt{k}
k,才能
A
C
\mathtt{AC}
AC。(别问我为什么,去官方题解看去,懒人专用链接)
时间复杂度: O ( log 2 n ) O(\log_2n) O(log2n)
空间复杂度: O ( 1 ) O(1) O(1)
#include <stdio.h>
#include <math.h>
long long n;int ans;
int main()
{
scanf("%lld",&n);
while((1LL<<ans)<=n)ans++;
printf("%d",--ans);
return 0;
}
注释: 1 < < n = 2 n 1<<n = 2^n 1<<n=2n。
Q u e s t i o n C : \mathtt{Question~C:} Question C:
基础
S
T
L
\mathtt{STL}
STL题,先
s
o
r
t
\mathtt{sort}
sort一下,在使用
n
e
x
t
p
e
r
m
u
t
a
t
i
o
n
\mathtt{next permutation}
nextpermutation求一下即可。(
C
+
+
S
T
L
\mathtt{C++ STL}
C++STL真好)
时间复杂度: O ( n log 2 n ) O(n\log_2n) O(nlog2n)
空间复杂度: O ( ∣ S ∣ ) O(|S|) O(∣S∣)
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char str[10];int k;
int main()
{
scanf("%s%d",str,&k);
sort(str,str+strlen(str));
while(--k)next_permutation(str,str+strlen(str));
puts(str);
return 0;
}
Q u e s t i o n D : \mathtt{Question~D:} Question D:
稍有难度。首先想到的是肯定不能枚举,那样时间复杂度太高( O ( n m ) O(nm) O(nm)),肯定超时(虽然给的是 2 s e c \mathtt{2sec} 2sec),所以我们需要以下这种方法。
-
分解 A \mathtt{A} A序列所有数的质因数,并将出现过的质因数当做下标在 h a s \mathtt{has} has数组中标记。
-
分解 1 ∼ m 1 \sim m 1∼m中所有数的质因数,若其质因数里在 h a s \mathtt{has} has中没有出现,那么就可以 p r i n t \mathtt{print} print了。
时间复杂度: O ( n n + m m ) O(n \sqrt n + m \sqrt m) O(nn+mm)
空间复杂度: O ( ? ? ? ) O(???) O(???)
#include <stdio.h>
#include <math.h>
#include <vector>
using namespace std;
int n,m,t,sz,has[100001];
vector<int> tmp,ans;
vector<int> coprime(int n)
{
if ( n == 1) return vector<int> ();
vector<int> answer;
int i = 0;
for (i = 1; i <= sqrt(n); i++)
{
if (!(n%i))
{
if(i!=1)answer.push_back(i);
answer.push_back(n / i);
}
}
// answer.push_back(n);
// for (i = 1; i * n <= m; i++)
// answer.push_back(i * n);
return answer;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&t);
tmp=coprime(t);
for(int i=0;i<tmp.size();i++)
if(tmp[i]<=m)
has[tmp[i]]=1;
}
for(int i=1;i<=m;i++)
{
tmp=coprime(i);
bool flag=true;
for(int j=0;j<tmp.size();j++)
if(has[tmp[j]])
{
flag=false;
break;
}
if(flag)ans.push_back(i);
}
printf("%d\n",ans.size());
for(int i=0;i<ans.size();i++)
printf("%d\n",ans[i]);
return 0;
}
完结撒花 ✿✿ヽ(°▽°)ノ✿✿