A - Swap Odd and Even
#include<bits/stdc++.h>
using namespace std;
int32_t main(){
string s;
cin >> s;
for( int i = 0 ; i + 1 < s.size() ; i += 2 )
swap( s[i] , s[i+1] );
cout << s;
return 0;
}
B - Call the ID Number
这道题的难点感觉是阅读。题意是每一个人\(i\)会叫一个人\(A_i\),从\(1\)开始,如果这个人被叫过,他就不会叫人,反之他就会叫人。输出所有没有被叫过的人。
#include<bits/stdc++.h>
using namespace std;
int32_t main(){
int n;
scanf("%d" , &n );
vector<int> a(n+1);
for( int i = 1 ; i <= n ; i ++ )
scanf("%d",&a[i]);
vector<bool> vis(n+1,false);
for( int i = 1 ; i <= n ; i ++ ){
if( vis[i] ) continue;
vis[ a[i] ] = true;
}
vector<int> k;
for( int i = 1 ; i <= n ; i ++ ){
if( vis[i] == false ) k.push_back(i);
}
printf("%d\n" ,k.size());
for( int i : k ) printf("%d " , i );
return 0;
}
C - Make Takahashi Happy
数据范围很小,直接 dfs 搜索就行,搜索的过程中记录一下经过路径上的值。
#include<bits/stdc++.h>
using namespace std;
int read(){
int x = 0 , ch = getchar();
while( ch < '0' || ch > '9' ) ch = getchar();
while( ch >= '0' && ch <= '9' ) x = (x<<3)+(x<<1)+ch-'0' , ch = getchar();
return x;
}
int n , cnt = 0 , m ;
vector<vector<int>> a;
void dfs( int x , int y , set<int> st ){
if( st.insert( a[x][y] ).second == false ) return;
if( x == n && y == m ){
cnt ++;
return ;
}
if( x+1 <= n ) dfs( x+1 , y , st );
if( y+1 <= m ) dfs( x , y+1 , st );
return;
}
int32_t main(){
n = read() , m = read();
a = vector<vector<int>>(n+1, vector<int>(m+1,0) );
for( int i = 1 ; i <= n ; i ++ )
for( int j = 1 ; j <= m ; j ++ )
a[i][j] = read();
dfs( 1 , 1 , set<int>() );
cout << cnt;
return 0;
}
D - Tying Rope
要照射到所有的位置就要保证向左照最靠右和向右照最靠左的相邻或相交才行。
#include<bits/stdc++.h>
using namespace std;
bool flag;
vector<vector<int>> e;
int n , m;
vector<int> vis , R , B ;
void dfs( int x ){
vis[x] = 1;
if( R[x] == 0 || B[x] == 0 ) flag = false;
for( auto v : e[x] ){
if( vis[v] ) continue;
dfs(v);
}
return;
}
int32_t main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
e = vector<vector<int>>(n+1 , vector<int>() );
vis = vector<int>(n+1) , R = vector<int>(n+1,0) , B = vector<int>(n+1,0);
int a , c;
char b , d;
for( ; m ; m -- ){
cin >> a >> b >> c >> d;
e[a].push_back(c) , e[c].push_back(a);
if( b == 'R' ) R[a] = 1;
else B[a] = 1;
if( d == 'R' ) R[c] = 1;
else B[c] = 1;
}
int x = 0 , y = 0;
for( int i = 1 ; i <= n ; i ++ ){
if( vis[i] ) continue;
flag = true;
dfs( i );
if( flag ) x ++;
else y ++;
}
cout << x << " " << y;
return 0;
}
E - Geometric Progression
令\(\sum_{i=0}^{x-1} A^i=sum(x)\)
如果\(x\)是偶数
\[sum(x)=a^0+a^1+\cdots+a^{\frac x2-1}+a^{\frac x2}(a^0+a^1+\cdots+a^{\frac x2-1})=(1+\frac x2)sum(x/2) \]如果\(x\)是奇数
\[sum(x)=a^0+a(a^0+a^1+a^{x-2})=1+a\times sum(x-1) \]快速幂加递归即可,复杂度\(O(\log^2(N))\)
#include<bits/stdc++.h>
using namespace std;
#define int long long
int a, x, m;
int power(int X, int Y) {
int ans = 1;
while (Y) {
if (Y & 1) ans = ans * X % m;
X = X * X % m, Y >>= 1;
}
return ans % m;
}
int sum(int x) {
if (x == 1) return 1;
if (x & 1) return (1 + a * sum(x - 1) ) % m;
return ((power(a, x / 2) + 1) % m * sum(x / 2) % m) % m;
}
int32_t main() {
cin >> a >> x >> m;
cout << sum(x)%m << "\n";
return 0;
}
标签:std,AtCoder,ch,return,Beginner,int,sum,vector,293
From: https://www.cnblogs.com/PHarr/p/17234336.html