题目链接
https://codeforces.com/contest/1543
A 题意
给出a,b可以同时对他们加一或减一,不能为负,求gcd最大情况,以及此时操作步数。注意GCD(x,0)为x,若可无限大输出0 0
A 思路
操作后设为a+k,b+k. gcd(a+k,b+k)=gcd(b+k,a-b)若a==b,答案为b+k,可到无穷大,输出0 0,否则答案为a-b,那么只需要找到b+k为a-b整数倍即可。会有两个最接近的答案,算出来取min
A 代码
#include<cstdio>
#include<iostream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<chrono>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl "\n"
#define int long long
//#define double long double
using namespace std;
typedef long long ll;
const int maxn=200505;
const int inf=0x3f3f3f3f;
int n,m,k;
int ans;
signed main(){
IOS
#ifndef ONLINE_JUDGE
freopen("IO\\in.txt","r",stdin);
freopen("IO\\out.txt","w",stdout);
#endif
int tn=1;
cin>>tn;
while(tn--){
cin>>n>>m;
if(n<m)swap(n,m);
if(n==m){
cout<<"0 0"<<endl;
}
else{
cout<<n-m<<' ';
int ma=inf;
int a=m/(n-m),b=(m-1)/(n-m)+1;
ma=min(abs(m-a*(n-m)),abs(b*(n-m)-m));
// for(int i=0;;i++){
// int d=i*(n-m)-m;
// d=abs(d);
// if(d<ma) ma=d;
// else break;
// }
cout<<ma<<endl;
}
}
}
B 题意
给出数组,可以任意重新赋值,满足总和不变即可,计算 ∑ i = 1 n ∑ j = i + 1 n ∣ a i − a j ∣ \sum _{i=1}^n\sum _{j=i+1}^n|ai−aj| i=1∑nj=i+1∑n∣ai−aj∣
B 思路
贪心。扰动法可知各个数字确定后排序不会影响答案。因此尽量平均分,最后可以分成a个i和b个i+1的形式答案为a*b
B 代码
#include<cstdio>
#include<iostream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<chrono>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl "\n"
#define int long long
//#define double long double
using namespace std;
typedef long long ll;
const int maxn=200505;
const int inf=0x3f3f3f3f;
int n,m,k;
int a[maxn];
int ans;
signed main(){
IOS
#ifndef ONLINE_JUDGE
freopen("IO\\in.txt","r",stdin);
freopen("IO\\out.txt","w",stdout);
#endif
int tn=1;
cin>>tn;
while(tn--){
cin>>n;
int s=0;
for(int i=1;i<=n;i++){
int t;
cin>>t;
s+=t;
}
if(s%n==0){
cout<<0<<endl;
continue;
}
else{
int d=s-(s/n)*n;
cout<<d*(n-d)<<endl;
}
}
}
C 题意
摸奖,三个奖概率为p1,p2,p3,给出系数v,若摸到第三号奖游戏结束,否则:
- 如果摸到的奖的概率大于v,则他的概率-=v,另外两个奖概率+=v/2。
- 如果摸到的奖的概率小于等于v,则他的概率归0,另外两个奖均分地加上他的概率,这个奖概率置为0后就再也不会增加概率了。
求结束步数期望。
C 思路
像期望dp那样思考转移方程,dp[i][j][k]=i * dp[i-v][j+v/2][k+v/2]+j * dp[i+v/2][j-v][k+v/2]+1。(置为0的情况无非就是个加个if,这里简化。)因为ijk是浮点数,考虑转成记忆化搜索,用map存放答案即可。但是看题解说v>0.1,状态较少可以直接暴搜。。总之就是个期望dp思想的搜索。
注意浮点数精度问题,这次也是整理了个浮点数精度板子
C 代码
#include<cstdio>
#include<iostream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<chrono>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl "\n"
#define int long long
#define double long double
using namespace std;
typedef long long ll;
const int maxn=200505;
const int inf=0x3f3f3f3f;
int n,m,k;
int ans;
double a,b,c,d;
double eps=1e-8;
map<pair<pair<double,double>,double>,double>mp;
inline int sgn( double x) {//和0比,大于返1等于返0小于返-1
return (x > eps) - (x < -eps);
}
//x = 0 写成 sgn(x) == 0
//x < 0 写成 sgn(x) < 0
//x > 0 写成 sgn(x) > 0
//x > y 写成 sgn(x - y) > 0
//x == y 写成 sgn(x - y) == 0
//x >= y 写成 sgn(x - y) >= 0
double dfs(double i,double j,double k){
if(mp[{{i,j},k}])
return mp[{{i,j},k}];
if(!sgn(k-1))
return 1;
double rec=0;
if(i){
if(j){
if(sgn(i-d)>0) rec+=i*dfs(i-d,j+d/2,k+d/2);
else rec+=i*dfs(0,j+i/2,k+i/2);
}
else{
if(sgn(i-d)>0) rec+=i*dfs(i-d,0,k+d);
else rec+=i;
}
}
if(j){
if(i){
if(sgn(j-d)>0) rec+=j*dfs(i+d/2,j-d,k+d/2);
else rec+=j*dfs(i+j/2,0,k+j/2);
}
else{
if(sgn(j-d)>0) rec+=j*dfs(0,j-d,k+d);
else rec+=j;
}
}
rec+=1;
return mp[{{i,j},k}]=rec;
}
signed main(){
IOS
#ifndef ONLINE_JUDGE
freopen("IO\\in.txt","r",stdin);
freopen("IO\\out.txt","w",stdout);
#endif
int tn=1;
cin>>tn;
while(tn--){
mp.clear();
mp[{{0,0},1}]=1;
cin>>a>>b>>c>>d;
cout<<fixed<<setprecision(12)<<dfs(a,b,c)<<endl;
}
}
D1 题意
此题为2进制异或。
初始密码x,属于0到n-1。你有n次机会猜。假如你猜了y,那么密码会变成z使得x^z=y。交互题让你猜密码
D1 思路
0到n-1,n次机会,我们直接枚举每个密码进行询问。但有个问题,我们需要抵消之前询问的影响。
根据二进制异或,易知z=x^y,那假如下一次我们猜k,答案还是错的,新密码z‘ =k ^ z,我们希望通过猜k而不是猜y+1来抵消之前的影响,也就是希望找到k使得z‘ = k^z = k^ x ^ y=x^ (y+1) 。解得z=(y+1)^y 。所以我们每次猜测密码为x时,输出询问x ^ (x-1)即可。
D1 代码
#include<cstdio>
#include<iostream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<chrono>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl "\n"
#define int long long
//#define double long double
using namespace std;
typedef long long ll;
const int maxn=200505;
const int inf=0x3f3f3f3f;
int n,m,k;
int ans;
signed main(){
IOS
#ifndef ONLINE_JUDGE
freopen("IO\\in.txt","r",stdin);
freopen("IO\\out.txt","w",stdout);
#endif
int tn=1;
cin>>tn;
while(tn--){
cin>>n>>m;
int last=0;
for(int i=0;i<n;i++){
cout<<(i^last)<<endl;
cout.flush();
cin>>k;
if(k==1) break;
else if(k==-1) exit(0);
else{
last=i;
}
}
}
}