Codeforces Round 1043 (Div.3)
比赛连接:Codeforces Round 1043 (Div.3)
A. Homework
题目链接:A - Homework
Vlad and Dima have been assigned a task in school for their English class. They were given two strings aaa and bbb and asked to append all characters from bbb to string aaa in any order. The guys decided to divide the work between themselves and, after lengthy negotiations, determined who would add each character from string bbb to aaa.
Due to his peculiarities, Vlad can only add characters to the beginning of the word, while Dima can only add them to the end. They add characters in the order they appear in string bbb. Your task is to determine what string Vlad and Dima will end up with.
Input
Each test consists of several test cases. The first line contains a single integer ttt (1≤t≤10001 \le t \le 10001≤t≤1000) — the number of test cases. The description of the test cases follows.
The first line contains an integer nnn (1≤n≤101 \le n \le 101≤n≤10) — the length of the string aaa.
The second line contains the string aaa, consisting of lowercase letters of the English alphabet.
The third line contains an integer mmm (1≤m≤101 \le m \le 101≤m≤10) — the length of the strings bbb and ccc.
The fourth line contains the string bbb, consisting of lowercase letters of the English alphabet.
The fifth line contains the string ccc, consisting of the characters ‘V’ and ‘D’ — the distribution of the characters of string bbb between Dima and Vlad. If cic_ici = ‘V’, then the iii-th letter is added by Vlad; otherwise, it is added by Dima.
Output
For each test case, output the string that will result from Dima and Vlad’s work.
这是一道很简单的模拟题,按照题意进行模拟即可。
// Problem: A. Homework
// Contest: Codeforces - Codeforces Round 1043 (Div. 3)
// URL: https://codeforces.com/contest/2132/problem/0
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pii pair<int,int>
#define fi first
#define se second
const int inf = 0x3f3f3f3f;void solve()
{int n;cin>>n;string s;cin>>s;int m;cin>>m;string t;cin>>t;string op;cin>>op;string ans = s;for(int i=0;i<op.size();i++){if(op[i] == 'D'){ans += t[i];}else{string tt;tt += t[i];ans = tt + ans;}}cout<<ans<<endl;
// cout<<fixed<<setprecision(x)<< ;
}signed main()// Don't forget pre_handle!
{IOSint T=1;cin>>T;while(T--) solve(); return 0;
}
B. The Secret Number
题目链接:B. The Secret Number
Vadim has thought of a number xxx. To ensure that no one can guess it, he appended a positive number of zeros to the right of it, thus obtaining a new number yyy. However, as a precaution, Vadim decided to spread the number n=x+yn = x + yn=x+y. Find all suitable xxx that Vadim could have thought of for the given nnn.
Input
Each test consists of several test cases. The first line contains a single integer ttt (1≤t≤104)(1 \le t \le 10^4)(1≤t≤104) — the number of test cases. The following lines describe the test cases.
In a single line of each test case, there is an integer nnn — the number spread by Vadim (11≤n≤1018)(11 \le n \le 10^{18})(11≤n≤1018).
Output
For each number nnn, output 000 if there are no suitable xxx. Otherwise, output the number of suitable xxx, followed by all suitable xxx in ascending order.
已知 n = x + y,其中 y 是 x 右边添加若干个 0 得到的数。假设 x 是 k 位数,在 x 右边添加 m 个 0 后,y = x * 10^m 。那么 n = x + x * 10^m = x * (1 + 10^m) ,所以 x = n / (1 + 10^m),需要满足 x 是整数,并且 y = x * 10^m 是 x 右边添加 m 个 0 得到的(即 x 和 y 的数字组成符合要求 )。
所以我们只需要暴力地求出来所有的可能的数即可。
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pii pair<int,int>
#define fi first
#define se second
const int inf = 0x3f3f3f3f;void solve()
{int n;cin>>n;vector<int> ans;int k = 10;for(int i=1;i<=18;i++){int x = 1LL + k;if(x > n) break;if(n % x == 0){int y = n / x;string t = to_string(y);string tt = t + string(i,'0');if(to_string(y * k) == tt) ans.push_back(y);}if(k * 10 > n) break;k *= 10;}if(ans.size() == 0){cout<<0<<endl;return ;}sort(ans.begin(),ans.end());cout<<ans.size()<<endl;for(auto &i : ans) cout<<i<<' ';cout<<endl;
// cout<<fixed<<setprecision(x)<< ;
}signed main()// Don't forget pre_handle!
{IOSint T=1;cin>>T;while(T--) solve(); return 0;
}
C1. The Cunning Seller (easy version)
题目链接:C1. The Cunning Seller (easy version)
这道题依旧是暴力,从大到小一直遍历就行了(感觉比B题还水)
// Problem: C1. The Cunning Seller (easy version)
// Contest: Codeforces - Codeforces Round 1043 (Div. 3)
// URL: https://codeforces.com/contest/2132/problem/C1
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pii pair<int,int>
#define fi first
#define se second
const int inf = 0x3f3f3f3f;void solve()
{int n;cin>>n;int ans = 0;for(int i=18;i>=0;i--){int k = pow(3LL,i+1LL) + i * pow(3LL,i-1);int num = pow(3,i);if(num > n) continue;int cnt = n / num;n -= cnt * num;ans += k * cnt;}// cout<<"n = "<<n<<endl;cout<<ans<<endl;
// cout<<fixed<<setprecision(x)<< ;
}signed main()// Don't forget pre_handle!
{IOSint T=1;cin>>T;while(T--) solve(); return 0;
}
C2. The Cunning Seller (hard version)
题目链接:C2. The Cunning Seller (hard version)
首先还是用C1的思路求出最少需要的交易次数,然后看能不能用交易次数来换交易金币数(贪心的思想),通过列式子就能发现贪心的思路:详解见代码:
// Problem: C2. The Cunning Seller (hard version)
// Contest: Codeforces - Codeforces Round 1043 (Div. 3)
// URL: https://codeforces.com/contest/2132/problem/C2
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pii pair<int,int>
#define fi first
#define se second
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define lbt(x) ((x) & (-x)) const int INF = 0x3f3f3f3f;
const int inf = 1e18;
const int N = 20;
int n,k;void solve()
{cin>>n>>k;int ans = 0;vector<int> cs(N,0);for(int i=19;i>=0;i--){int num = pow(3,i);if(num > n) continue;int cnt = n / num;n -= cnt * num;ans += cnt;cs[i] = cnt;//用cs数组存当前的i所需要的交易次数}if(ans > k)//如果最少的操作次数都达不到的话就是-1啦{cout<<"-1"<<endl;return ;}k -= ans;//计算还能“浪费多少次数来使得花费更小”for(int i=19;i>=1;i--){if(k < 2) break;//因为是三进制 所以我们可以用一次高位交易抵三次的低位交易 所以每一次的贪心操作都能“浪费2次交易”int x = k / 2;int y = cs[i];//k小于2了就贪心不了了,所以就要退出int mi = min(x,y);cs[i] -= mi;cs[i-1] += 3 * mi;//每一次贪心就相当于是低位上多了三次交易k -= 2 * mi;//浪费了2 * mi次交易}/*想一下为什么这样就贪心了?首先对于x来说:我们所需要花费的金币数是:3^(x+1) + x*3^(x-1)对于更高位来说:我们所需要的金币数是:3^(x+2) + (x+1)*(3^x)我们知道对于三进制来说,一次高位操作等于3个低位操作(高位 = 低位 * 3)那么我们计算3次低位所需要花费的金币数:3 * (3^(x+1) + x*3^(x-1))即:3^(x+2) + x * 3^x我们与之和高位所需要花费的金币数来进行比较,是不是少了3^x?这就是贪心的过程:浪费交易次数使得高位的交易尽量少 尽量转移到低位上去*/int res = 0;for(int i=0;i<=19;i++){int t = pow(3, i+1) + i * pow(3, i-1);res += t * cs[i];}cout<<res<<endl;
// cout<<fixed<<setprecision(x)<< ;
}signed main()// Don't forget pre_handle!
{IOSint T=1;cin>>T;while(T--) solve(); return 0;
}