Atcoder Beginner 293 题解
A - Swap Odd and Even
注意下标开始的序号
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
int len=s.length();
for(int i=1;i<=len/2;i++){
swap(s[2*i-2],s[2*i-1]);
}
cout<<s;
}
B - Call the ID Number
模拟,可以另设一个数组标记出哪一个人没有被点到
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int a[N];
bool b[N];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
int num=0;
for(int i=1;i<=n;i++){
if(b[i]==true) continue;
b[a[i]]=true;
}
for(int i=1;i<=n;i++){
if(b[i]==false) num++;
}
cout<<num<<endl;
for(int i=1;i<=n;i++){
if(b[i]==false) cout<<i<<' ';
}
}
C - Make Takahashi Happy
DFS
一看到题,就想到如果我把Takahshi走的线路全部都找出来,然后判断是否这个线路上有相同的数字,没有的话线路数加一。再来看数据范围,显然是可以的,我用map来保存出现过的数字,先判断这个数字是否出现过,如果未出现再走到这个格子上,因为只能向下和向右走,也不用考虑这个格子已经走过了,又再走一遍的问题。
#include<bits/stdc++.h>
using namespace std;
int a[12][12];
int summ=0;
int h,w;
map<int,int>ma;
void dfs(int x,int y){
if(x==h&&y==w){
if(ma[a[x][y]]==1) summ++;
return;
}
if(ma[a[x+1][y]]==0&&x+1<=h&&y<=w){
ma[a[x+1][y]]++;
dfs(x+1,y);
ma[a[x+1][y]]--;
}
if(ma[a[x][y+1]]==0&&x<=h&&y+1<=w){
ma[a[x][y+1]]++;
dfs(x,y+1);
ma[a[x][y+1]]--;
}
}
int main(){
cin>>h>>w;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
cin>>a[i][j];
}
}
ma[a[1][1]]++;
dfs(1,1);
cout<<summ;
}
D - Tying Rope
判环问题,但这个题目要求比较特殊,因为题目中有这样一句话用过的绳子一端不能重复使用,这句话就决定了这个环肯定是首尾相连的环,即每个连通分量是一个环,而不是部分节点形成一个环。在首位相连的环中每一个节点的入度都是2,所以可以统计出每个节点的入度,然后利用BFS判断每一个连通分量的每个节点的度。如果这个连通分量每个节点的度都是2,则说明这个连通分量是一个环。
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int deg[N];
int main(){
int n,m;
cin>>n>>m;
vector<vector<int>>g(n,vector<int>());
while(m--){
int a,c;
char b,d;
cin>>a>>b>>c>>d;
a--;
c--;
g[a].push_back(c);
g[c].push_back(a);
deg[a]++;
deg[c]++;
}
int x=0,y=0;
vector<bool>used(n);
for(int i=0;i<n;i++){
if(!used[i]){
queue<int>q;
used[i]=true;
q.push(i);
bool f=true;
while(q.size()){
int t=q.front();
q.pop();
if(deg[t]!=2) f=false;
for(int v : g[t]){
if(!used[v]){
used[v]=true;
q.push(v);
}
}
}
if(f) x++;
else y++;
}
}
cout<<x<<' '<<y;
}
E - Geometric Progression
定义 a n = ∑ i = 0 n − 1 A i a_n = \sum_{i=0}^{n-1} A^i an=∑i=0n−1Ai, a 0 = 0 a_0=0 a0=0并且 a n + 1 = A a n + 1 a_{n+1}=Aa_n+1 an+1=Aan+1,我们想求的是 a x m o d M a_x\ mod\ M ax mod M
通过上面的等式, ( a n + 1 1 ) = ( A 1 0 1 ) ( a n 1 ) \begin{pmatrix} a_{n+1} \\ 1 \end{pmatrix} = \begin{pmatrix} A\ 1 \\ 0\ 1 \end{pmatrix} \begin{pmatrix} a_n \\ 1 \end{pmatrix} (an+11)=(A 10 1)(an1)
从 a 0 = 0 a_0=0 a0=0,我们有 ( a X 1 ) = ( A 1 0 1 ) X ( 0 1 ) \begin{pmatrix} a_X \\ 1 \end{pmatrix} = \begin{pmatrix} A\ 1 \\ 0\ 1 \end{pmatrix}^X \begin{pmatrix} 0 \\ 1 \end{pmatrix} (aX1)=(A 10 1)X(01)
( A 1 0 1 ) X \begin{pmatrix} A\ 1 \\ 0\ 1 \end{pmatrix}^X (A 10 1)X m o d M mod\ M mod M 的值可以利用快速幂在 l o g ( x ) log(x) log(x)的时间内找到,因此该题的时间复杂度为 l o g ( x ) log(x) log(x)
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
vector<vector<ll>> mat_mul(vector<vector<ll>> a, vector<vector<ll>> b, ll mod) {
// 行列乗算
int n = a.size();
vector<vector<ll>> res(n, vector<ll>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
res[i][j] += a[i][k] * b[k][j];
res[i][j] %= mod;
}
}
}
return res;
}
vector<vector<ll>> mat_pow(vector<vector<ll>> a, ll b, ll mod) {
// Matrix exponentiation
int n = a.size();
vector<vector<ll>> res(n, vector<ll>(n));
for (int i = 0; i < n; i++) res[i][i] = 1;
while (b) {
if (b & 1) res = mat_mul(res, a, mod);
a = mat_mul(a, a, mod);
b >>= 1;
}
return res;
}
int main() {
ll a, x, m;
cin >> a >> x >> m;
vector<vector<ll>> f = { {a,1},{0,1} };
vector<vector<ll>> g = mat_pow(f, x, m);
cout << g[0][1] << '\n';
}