A Exciting Bets
题目大意
给两个数a, b, 这两个数能同时+1或者-1,求这两个数的最大公约数,以及变到最大公约数最少需要几步
主要思路
a和b不同时,最大公约数d都是a和b的约数,a = x * d, b = y * d且x != y,也就是说a和b的差是一定的,且a和b中至少相差一个d,所以d的最大值为|a - b|
a和b相同时,最大公约数为a,b本身,由于能无限增大,输出0 0
接下来考虑第二问,设最大公约数|a - b| = d,判断a和b至少需要几步能变到最大公约数为d,由于a,b同时变,所以判断一个即可,用a % d或者b % d得到余数t,答案就是min(t, d - t),含义就是至少需要几步能变到最大公约数为d的倍数
AC代码
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <string>
#include <cmath>
#include <unordered_map>
#include <queue>
#include <map>
using namespace std;
#define int long long
#define debug(a) cout << #a << " = " << a << endl;
#define x first
#define y second
typedef pair<int, int> P;
const int N = 200010;
int n, a[N];
signed main(void)
{
int T;
cin >> T;
while(T--)
{
int a, b;
cin >> a >> b;
int t = abs(a - b);
if(t == 0)
{
puts("0 0");
}
else
{
cout << t << ' ';
cout << min(a % t, t - a % t) << endl;
}
}
return 0;
}
B Customising the Track
题目大意
给定一个长度为n的数组,你可以将任意调整数组,保持数组总和不变求 ∑ i = 1 n ∑ j = i + 1 n ∣ a i − a j ∣ \sum^{n}_{i = 1}{\sum^{n}_{j = i + 1}{|ai - aj|}} ∑i=1n∑j=i+1n∣ai−aj∣最小值
主要思路
求平均值,先平均分,再将多出的几个数放到开头或者结尾即可
AC代码
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <string>
#include <cmath>
#include <unordered_map>
#include <queue>
#include <map>
using namespace std;
#define int long long
#define debug(a) cout << #a << " = " << a << endl;
#define x first
#define y second
typedef pair<int, int> P;
const int N = 200010;
int n, a[N];
signed main(void)
{
int T;
cin >> T;
while(T--)
{
int sum = 0;
cin >> n;
for(int i = 0; i < n; i++) cin >> a[i], sum += a[i];
sum %= n;
//debug(sum)
cout << sum * (n - sum) << endl;
}
return 0;
}
C Need for Pink Slips
题目大意
给出概率p1,p2,p3,参数v ,每轮从p1, p2, p3中抽一个,抽到p1, p2则减去min(p1, v),平分给另外两个概率,概率小于1e-6视为不合法不能被分配,抽到p3游戏结束,求轮数的期望
主要思路
dfs模拟看代码
AC代码
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <string>
#include <cmath>
#include <unordered_map>
#include <queue>
#include <map>
using namespace std;
#define int long long
#define debug(a) cout << #a << " = " << a << endl;
#define x first
#define y second
typedef pair<int, int> P;
const int N = 200010;
double p1, p2, p3, v;
double ans;
void dfs(int u, double p1, double p2, double p3, double now)
{
ans += u * now * p3;
if(p1 == 0 && p2 == 0) return ;
if(p1 >= 1e-6)
{
double x = min(p1, v);
if(p2 >= 1e-6) dfs(u + 1, p1 - x, p2 + x / 2, p3 + x / 2, now * p1);
else dfs(u + 1, p1 - x, 0, p3 + x, now * p1);
}
if(p2 >= 1e-6)
{
double x = min(p2, v);
if(p1 >= 1e-6) dfs(u + 1, p1 + x / 2, p2 - v, p3 + x / 2, now * p2);
else dfs(u + 1, 0, p2 - x, p3 + x, now * p2);
}
}
signed main(void)
{
int T;
cin >> T;
while(T--)
{
ans = 0;
cin >> p1 >> p2 >> p3 >> v;
dfs(1, p1, p2, p3, 1);
printf("%.8lf\n", ans);
}
return 0;
}
D1 RPD and Rap Sheet (Easy Version)
题目大意
给定一个数n和一个初始密码k(0 <= k < n),每次你猜一个密码x时密码都会变成 x ^ k,让你在n次内猜出密码
主要思路
交互题,有两种思路。
- 将密码"固定",遍历n次猜出密码
- 将密码“逼近”到一个固定的数
(第二种我也不知道能不能做,只是在考虑时有的思路)
那么我们就用第一种思路来做
首先考虑密码会变那么怎么将密码“固定”呢,我们每次猜的数x为i ^ i - 1(0 <= i < n)由于密码会改变,我们每次异或相邻的两个数前面的数会被异或两次,相当于没异或,于是密码永远为k ^ i,而我们猜的数也为i - 1 ^ i,在遍历时 总有一个i满足要求
AC代码
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <string>
#include <cmath>
#include <unordered_map>
#include <queue>
#include <map>
using namespace std;
#define int long long
#define debug(a) cout << #a << " = " << a << endl;
#define x first
#define y second
typedef pair<int, int> P;
const int N = 200010;
int n, a[N];
signed main(void)
{
int T;
cin >> T;
while(T--)
{
int n, k;
cin >> n >> k;
for(int i = 0; i < n; i++)
{
int x = max((int)0, i - 1) ^ i;
cout << x << endl;
int t;
cin >> t;
if(t == 1) break;
}
}
return 0;
}
待补充!