Codeforces Round #647 (Div. 2) A、B、C、D

A - Johnny and Ancient Computer 题目链接
将b>a的情况调换成b

#include
#include
#include
#include
#include
#define ll long long
using namespace std;
ll t,i,n,m,k,j,x,a,b;
int main()
{
 cin>>t;
 while(t--){
  cin>>a>>b;
  n=0;
  if(a==b) {cout<<0<<endl;continue;}
  if(a<b) {
     k=a;
     a=b;
     b=k; 
   }
  
  while(b<a){
   if(b*8<=a) b*=8;
   else if(b*4<=a) b*=4;
   else if(b*2<=a) b*=2;
   else if(b*2>a) break;
      n++;
  }
  if(b==a) cout<<n<<endl;
  else cout<<"-1"<<endl; 
 } 
 return 0;
}

B. Johnny and His Hobbies 题目链接
枚举就行了

#include 
#include 
#include 
#include 
#include 
#include 
#define LL long long
#define PI 3.141592653589
using namespace std;
LL t,n,m,x,k;
int a[10005],b[1030];
int main()
{
 cin>>t;
 while(t--)
 {
  cin>>n;
  k=0;
  for(int i=0;i<n;i++)
    cin>>a[i];
    sort(a,a+n);
  for(int i=1;i<1024;i++){
   x=1;
   for(int j=0;j<n;j++)
     b[j]=a[j]^i;
   sort(b,b+n);
  for(int j=0;j<n;j++){
   if(b[j]!=a[j]) {x=0;break;} 
  }
     if(x) {k=i;break;} 
  }
  if(k) cout<<k<<endl;
  else cout<<"-1"<<endl;
    } 
 return 0;
}

C. Johnny and Another Rating Drop题目链接
数学

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
ll t,n,m,k,l;
int main()
{
 cin>>t;
 while(t--)
 {
  cin>>n;
  ll num=1,sum=0;
  while(n/num){
   sum+=n/num;
   num*=2;
  }
     cout<<sum<<endl;
 } 
 return 0;
} 

D. Johnny and Contribution**题目链接**

#include
#include
using namespace std;

bool vis[500009]={false};
vector<int> in[500009];
vector<int> blog[500009];
int topic[500009];
int main() {
 vector<pair<int,int> > edges;
 vector<int> ans;
 bool ok = true;
 int n,m;
 scanf("%d %d",&n,&m);
 for(int i=0;i<m;i++) {
  int u,v;
  scanf("%d %d",&u,&v);
  edges.push_back(make_pair(u,v));
 }
for(int i=1;i<=n;i++) {
  scanf("%d",&topic[i]);
  blog[topic[i]].push_back(i);
 }
 for(int i=0;i<m;i++) {
  int u = edges[i].first;
  int v = edges[i].second;
  if(topic[u]==topic[v]) {
   ok = false;
   break;
  }
  if(topic[u] > topic[v]) {
   swap(u,v);
  }
  in[v].push_back(u);
 }
for(int i=1;i<=n && ok;i++) {
  int c = 0;
  for(int j=0;j<in[i].size();j++) {
   int t = topic[in[i][j]];
   if(!vis[t]) {
    vis[t]=true;
    c++;
   }
  }
  for(int j=0;j<in[i].size();j++) {
   int t = topic[in[i][j]];
   vis[t]=false;
  }
  ok = ok && (c==topic[i]-1);
 }
 if(!ok) {
  printf("%d\n", -1);
  return 0;
 }
for(int i=1;i<=n;i++) {
  for(int j=0;j<blog[i].size();j++) {
   printf("%d ", blog[i][j]);
  }
 }
 printf("\n");
 return 0;
}
 

你可能感兴趣的:(Codeforces Round #647 (Div. 2) A、B、C、D)