「CF1246X」Codeforces Round #596 (Div. 1)
A. p-binary
最终答案不超过 logn,枚举答案 i,找出 n – i * p 在二进制下 1 的个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#include<map> #include<cmath> #include<ctime> #include<queue> #include<cstdio> #include<vector> #include<bitset> #include<cstring> #include<iostream> #include<algorithm> #define pa pair<int,int> #define ll long long #define inf 1000000000 #define mod 1000000007 using namespace std; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n, p; int bin[32] = {0}; bool check(int x) { int m = n - x * p; int res = 0; if(m < x)return 0; for(int i = 0; i <= 31; i++) if(m & bin[i]) { x--; if(x < 0)return 0; } return 1; } int main() { bin[0] = 1; for(int i = 1; i <= 31; i++) bin[i] = bin[i - 1] << 1; n = read(); p = read(); for(int i = 1; i <= 100; i++) if(check(i)) { cout << i << endl; return 0; } puts("-1"); return 0; } |
B. Power Products
想了一个比较复杂的做法
先把所有在 10^10 以内的,能表示成 x^k 的数存起来
若 k = 2,对于每个数 ai,把 ai 的平方因子除掉以后得到 y,和它配对的数一定是 y * t^2
若 k > 2,10 ^ 10 内 x^k 数至多 2 万个,枚举一个数,暴力找另一个和它配对的数
比较简单的做法是,先把每个数做质因数分解,把指数取模 k 以后,找与它互补的数的倍数计数即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#include<map> #include<cmath> #include<ctime> #include<queue> #include<cstdio> #include<vector> #include<bitset> #include<cstring> #include<iostream> #include<algorithm> #define pa pair<int,int> #define ll long long #define inf 1000000000 #define mod 1000000007 using namespace std; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n, k; ll a[100005], v[100005]; ll ans; vector<ll> ve; int main() { n = read(); k = read(); for(int i = 1; i <= n; i++) { a[i] = read(); v[a[i]]++; } for(int i = 1; i <= 100000; i++) { ll x = i; for(int j = 1; j < k; j++) { x = x * i; if(x > 10000000000LL)break; } if(x <= 10000000000LL)ve.push_back(x); else break; } if(k >= 3) { for(int i = 1; i <= 100000; i++) if(v[i]) for(int j = 0; j < ve.size() && ve[j] / i <= 100000; j++) if(ve[j] % i == 0) { int t = ve[j] / i; if(t < i)continue; if(i == t) ans += v[i] * (v[i] - 1) / 2; else ans += v[i] * v[t]; } } else { for(int i = 1; i <= 100000; i++) if(v[i]) { ll x = i; for(int j = 2; j <= 400; j++) while(x % (j * j) == 0) x /= (j * j); for(int j = 0; j < ve.size() && ve[j] * x <= 100000; j++) { int t = ve[j] * x; if(t < i)continue; if(i == t) ans += v[i] * (v[i] - 1) / 2; else ans += v[i] * v[t]; } } } cout << ans << endl; return 0; } |
Subscribe