A. GCD vs LCM
- 题目大意
给定一个数n,使得满足:
a + b + c + d = n , a n d a+b+c+d=n, and a+b+c+d=n,and g c d ( a , b ) = l c m ( c , d ) gcd(a,b)=lcm(c,d) gcd(a,b)=lcm(c,d),
输出满足条件的 a , b , c , d a,b,c,d a,b,c,d. - 思路
观察发现: g c d ( 任 何 数 , 1 ) = 1 , l c m ( 1 , 1 ) = 1 gcd(任何数,1) = 1, lcm(1,1)=1 gcd(任何数,1)=1,lcm(1,1)=1,因此答案为 a = n − 3 , b = 1 , c = 1 , d = 1 a=n-3,b=1,c=1,d=1 a=n−3,b=1,c=1,d=1. - code
#include<bits/stdc++.h>
using namespace std;
#define N
int t,n;
int main()
{
cin>>t;
while(t--)
{
scanf("%d",&n);
printf("%d 1 1 1\n",n-3);
}
return 0;
}
B. Array Cloning Technique
- 题目大意
给定一个长度为n的数组 a,现在有两种操作:
$1.Choose any array and clone it. After that there is one more copy of the chosen array.
$2.Swap two elements from any two copies (maybe in the same copy) on any positions.
问最少操作多少次使得其中一个copy中数字全部相同。 - 思路
贪心:将数量最多的数字反复copy并转移到一个copy中,直到这个copy数字全部一致为止。 - code
#include<bits/stdc++.h>
using namespace std;
#define N 100005
#define inf 1000000009
#define ll long long
ll t,n,a[N];
int main()
{
cin>>t;
while(t--)
{
scanf("%d",&n);
map<ll,ll> mp;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
mp[a[i]]++;
}
int maxn=-inf;
for(auto [u,v]:mp) if(v>maxn) maxn=v;
if(n==maxn) cout<<0<<endl;
else
{
int tot=maxn,op=0;
while(1)
{
if(tot>=n) break;
op++;
op+=maxn;
tot+=maxn;
maxn*=2;
}
if(tot>n) cout<<op-(tot-n)<<endl;
else cout<<op<<endl;
}
}
return 0;
}
C. Tree Infection
- 题目大意
有一棵树,每一秒中,都可以指定其中一个节点得病,并且亲兄弟之间只要有人得病,每秒钟就会有一个人被传染。问最少多少秒可以使得树中的所有节点都得病。
- 思路
不同的兄弟们之间是相互独立的,因此可以将问题转化成:亲兄弟们分成一组,这些组被感染之后每秒钟里面的节点数会减1。显然贪心的想,每次感染人数最多的一组是最优的,并且同时会衰减1,衰减到0以下就表明全部被感染了。 - code
#include<bits/stdc++.h>
using namespace std;
#define N 200005
int t,n,g[N],x;
int main()
{
cin>>t;
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++) g[i]=0;
vector<int> res;
for(int i=2;i<=n;i++)
{
scanf("%d",&x);
g[x]++;
}
int num=0,t=0;
for(int i=1;i<=n;i++) if(g[i]) res.push_back(g[i]);
res.push_back(1);
sort(res.begin(),res.end());
priority_queue<int> q;
for(int i=0;i<res.size();i++) q.push(res[i]-i-1);
num=q.size();
while(!q.empty())
{
if(q.top()<=t) break;
num++,t++;
q.push(q.top()-1);
q.pop();
}
cout<<num<<endl;
}
return 0;
}
D. GCD Guess
- 题目大意
交互题:
提问:“? a b”返回一个值gcd(x+a, x+b)
回答:“!x”输出你已经猜中的x是多少
最多有30次提问机会。
交互题一般都是二分或者二进制了,这样保证在30次提问以内就很合理。
- 思路
首先一个想法是: g c d ( x + 0 , x + 1 ) = = 0 ∣ ∣ 1 gcd(x+0,x+1)==0||1 gcd(x+0,x+1)==0∣∣1可以确定 x x x第一位为0或1(==0则说明第一位为0,==1则说明第一位为1)。
继续向高位推进,将判断过的低位全部置为0(用一个数字r记录下低位数字),则 g c d ( x + 0 − r , x + 2 k − r ) = = 0 ∣ ∣ 1 gcd(x+0-r,x+2^k-r)==0||1 gcd(x+0−r,x+2k−r)==0∣∣1可以确定 x x x第 k + 1 k+1 k+1位为0或1(==0则说明第一位为0,==1则说明第一位为1)。
但是,由于题目中保证了 a a a和 b b b的范围为 [ 1 , 2 31 ] [1,2^{31}] [1,231],因此可以通过a和b分别再加 2 k + 1 2^{k+1} 2k+1即可,既不影响 g c d gcd gcd的结果也契合了 a a a, b b b的范围。 - code
#include<bits/stdc++.h>
using namespace std;
int t,n;
// 交互题 不要用printf 和 scanf
int main()
{
cin>>t;
while(t--)
{
vector<int> ans;
int a,b,r=0,temp=0;
for(int i=0;i<30;i++) //不能超过范围
{
a = (1<<i)-temp;
b = (1<<i)+(1<<(i+1))-temp;
cout<<"?"<<" "<<a<<" "<<b<<endl;
cin>>r;
if(r==(1<<(i+1))) ans.push_back(1),temp+=(1<<i); //注意 是确定了哪一位
else ans.push_back(0);
}
cout<<"!"<<" "<<temp<<endl;
}
return 0;
}
E. MinimizOR
- 题目大意
- 思路
- code