codeforces 596

C

题意

定义p-binary为2^x+p

现在给你一个数x,和一个p。

问你最少用多少个p-binary能构造出x,如果没有输出-1

题解

转化为:

x = 2^x1 + 2^x2 + ... + 2^xn + n*p

首先我们知道任何数都能用二进制表示,如果p=0的话,肯定是有解的。那么答案最少都是x的2进制1的个数。

另外什么情况无解呢,即x-n*p<0的时候肯定无解,可以更加有优化为x-n*p<n的时候无解。

答案实际上就是n,我们从小到大枚举n,然后check现在的2进制中1的个数是否小于等于n。

#include<bits/stdc++.h>
using namespace std;

int Count(int x){
    int number=0;
    for(;x;x-=x&-x){
        number++;
    }
    return number;
}
int main(){
    int n,p,ans=0;
    scanf("%d%d",&n,&p);
    while(1){
        n-=p;
        ans++;
        int cnt=Count(n);
        if(ans>n){
            cout<<"-1"<<endl;
            return 0;
        }
        if(cnt<=ans){
            cout<<ans<<endl;
            return 0;
        }
    }
}

  D

You are given n positive integers a1,…,an, and an integer k≥2. Count the number of pairs i,j such that 1≤i<j≤n, and there exists an integer x such that ai⋅aj=xk.

In the sample case, the suitable pairs are:

a1⋅a4=8=2^3;
a1⋅a6=1=1^3;
a2⋅a3=27=3^3;
a3⋅a5=216=6^3;
a4⋅a6=8=2^3.

题意

题目这么短,我就偷懒不翻译了吧。。

题解

首先我们质因数分解后,如果两个数的质因数分解后的每个数的因子个数都是k的倍数,那么就说明有解。

于是我们先对每个数质因数分解一下,然后再用一个vector去找一下配对的那一个是哪个。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int n,k;
int a[maxn];
map<vector<pair<int,int> >,int>H;
int main(){
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    long long ans = 0;
    for(int i=0;i<n;i++){
        string tmp="";
        vector<pair<int,int> >fac;
        for(int now=2;now*now<=a[i];now++){
            int number=0;
            while(a[i]%now==0){
                a[i]/=now;
                number+=1;
            }
            if(number%k)
                fac.push_back(make_pair(now,number%k));
        }
        if(a[i]>1)fac.push_back(make_pair(a[i],1%k));
        vector<pair<int,int> >fac2;
        for(int j=0;j<fac.size();j++){
            fac2.push_back(make_pair(fac[j].first,k-fac[j].second));
        }
        ans+=H[fac2];
        H[fac]++;///类似前缀的方式可以避免重复选到以下情况【ai,aj】【aj,ai】;
    }
    cout<<ans<<endl;
}

 

猜你喜欢

转载自www.cnblogs.com/hgangang/p/12376419.html
今日推荐