atcoder abc234 a-f
A.Weired Function
简单的计算,记得开long long
#include<iostream>
#include<cmath>
#define int long long
using namespace std;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int x;
cin>>x;
int x1 = pow(x,2) + 2 * x + 3;
//cout<<x1<<"|||\n";
int x2 = x1 + x;
//cout<<x2<<"|||\n";
int x3 = pow(x2,2) + 2 * x2 + 3;
//cout<<x3<<"|||\n";
int x4 = pow(x1,2) + 2 * x1 + 3;
//cout<<x4<<"|||\n";
int x5 = x3 + x4;
//cout<<x5<<"|||\n";
int ans = pow(x5,2) + 2 * x5 + 3;
cout<<ans;
}
B.Longest Segment
O
(
n
2
)
O(n^2)
O(n2) 寻找最大直线距离
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
vector<pair<int,int>> v;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
scanf("%d",&n);
for(int i = 1;i <= n;i ++ ){
int x,y;
scanf("%d %d",&x,&y);
v.push_back({x,y});
}
double maxx = 0;
for(int i = 0;i < v.size();i ++ ){
for(int j = 0;j < v.size();j ++ ){
maxx = max(maxx,sqrt(abs(v[i].second - v[j].second) * abs(v[i].second - v[j].second) + abs(v[i].first - v[j].first) * abs(v[i].first - v[j].first)));
}
}
printf("%.10lf",maxx);
}
C.Happy New Year
寻找一串由0和2构成的数字中第n个数
本来是在oeis上找到的规律,但是,t了
后来发现位运算就可以解决
#include<iostream>
#include <cmath>
#include<cstring>
#include<algorithm>
#define int long long
using namespace std;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int x;
cin>>x;
string ans;
while(x){
if(x & 1) ans += '2';
else ans += '0';
x >>= 1;
}
reverse(ans.begin(),ans.end());
cout<<ans;
}
D - Prefix K-th Max
寻找一串数字中第n大的数
大根堆的运用
#include<iostream>
#include <cmath>
#include<cstring>
#include<algorithm>
#include<queue>
//#define int long long
using namespace std;
const int N = 5e5 + 10;
int a[N];
priority_queue<int,vector<int>,greater<int>> q;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,k;
cin>>n>>k;
for(int i = 1;i <= n;i ++ ){
//int x;
cin>>a[i];
if(i <= k){
q.push(a[i]);
}
}
cout<<q.top()<<"\n";
for(int i = k + 1;i <= n;i ++ ){
if(a[i] > q.top()){
q.pop();
q.push(a[i]);
}
if(q.size() >= k) cout<<q.top()<<"\n";
}
}
E - Arithmetic Number
寻找最小的大于n的数,并且该数的各个数位组成的数组是等差数列
从-9到9枚举公差,将最小的符合条件的数输出
#include<iostream>
#include <cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#define int long long
using namespace std;
const int N = 20;
int a[N];
int b[N];
int cnt,ans;
int n;
bool check(int n){
int d = a[2] - a[1];
for(int i = 1;i < n;i ++ ){
if(a[i + 1] - a[i] != d) return 0;
}
return 1;
}
bool check1(int x){
int cnt = 0;
memset(b,0,sizeof b);
while(x){
b[++ cnt] = x % 10;
x /= 10;
}
int d = b[2] - b[1];
for(int i = 1;i < cnt;i ++ ){
if(b[i + 1] - b[i] != d) return 0;
}
return 1;
}
void check(int x,int d){
//cout<<x<<"|||\n";
int t = x;
for(int i = 2;i <= cnt;i ++ ){
x -= d;
if(x < 0 || x > 9) return;
t = t * 10 + x;
}
if(check1(t) && t >= n){
//cout<<t<<"|||\n";
ans = min(ans,t);
//cout<<ans<<"|||\n";
}
}
//int b[10005];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
//int x;
cin>>n;
int te = n;
while(n){
a[++ cnt] = n % 10;
n /= 10;
}
n = te;
reverse(a + 1,a + cnt + 1);
if(check(n)) cout<<n;
else{
ans = 1e18;
for(int i = -9;i <= 9;i ++ ){
check(a[1],i);
check(a[1] + 1,i);
}
cout<<ans<<"\n";
}
}
F - Reordering
给出一串字符,求解该字符串的子集个数
组合数学+dp
第一维表示已经用了多少个字母表中的字母
第二维表示用了多少个字母
k是当前第i个字母表中的字母在串中出现的次数
那么可以推断
f
[
i
+
1
]
[
j
]
=
f
[
i
+
1
]
[
j
]
+
f
[
i
]
[
j
−
k
]
∗
C
j
k
f[i + 1][j] = f[i + 1][j] + f[i][j - k] * C_{j}^{k}
f[i+1][j]=f[i+1][j]+f[i][j−k]∗Cjk
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define int long long
using namespace std;
const int mod = 998244353;
const int MX = 5005;
int f[30][MX];
int cnt[30];
int fac[MX];
int inv[MX];
int finv[MX];
void fac_init(){
fac[0] = fac[1] = 1;
inv[1] = 1;
finv[0] = finv[1] = 1;
for(int i = 2;i < MX;i ++ ){
fac[i] = fac[i - 1] * i % mod;
inv[i] = mod - mod / i * inv[mod % i] % mod;
finv[i] = finv[i - 1] * inv[i] % mod;
}
}
int c(int x,int y){
if(x < y || x < 0 || y < 0) return 0;
return fac[x] * finv[y] % mod * finv[x - y] % mod;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
fac_init();
string str;
cin>>str;
//cout<<str;
int t = str.size();
for(int i = 0;i < t;i ++ ){
cnt[str[i] - 'a'] ++;
}
f[0][0] = 1;
for(int i = 0;i < 26;i ++ ){
for(int j = 0;j <= t;j ++ ){
for(int k = 0;k <= min(j,cnt[i]);k ++ ){
f[i + 1][j] += f[i][j - k] * c(j,k);
f[i + 1][j] %= mod;
}
}
}
int ans = 0;
for(int i = 1;i <= t;i ++ ){
ans += f[26][i];
ans %= mod;
}
cout<<ans;
}