codeforces #595 div2 pp题解

A. Forgetting Things

Description

给出a和b的最高位,输出一组可能的a+1=b

Solution

判断a=b,a=b-1,b=9&a=1三种情况

codeforces #595 div2 pp题解
 1 #include <algorithm>
 2 #include <cctype>
 3 #include <cmath>
 4 #include <cstdio>
 5 #include <cstdlib>
 6 #include <cstring>
 7 #include <iostream>
 8 #include <map>
 9 #include <numeric>
10 #include <queue>
11 #include <set>
12 #include <stack>
13 #if __cplusplus >= 201103L
14 #include <unordered_map>
15 #include <unordered_set>
16 #endif
17 #include <vector>
18 #define lson rt << 1, l, mid
19 #define rson rt << 1 | 1, mid + 1, r
20 #define LONG_LONG_MAX 9223372036854775807LL
21 #define pblank putchar(' ')
22 #define ll LL
23 #define fastIO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
24 using namespace std;
25 typedef long long ll;
26 typedef long double ld;
27 typedef unsigned long long ull;
28 typedef pair<int, int> P;
29 int n, m, k;
30 const int maxn = 1e5 + 10;
31 template <class T>
32 inline T read()
33 {
34     int f = 1;
35     T ret = 0;
36     char ch = getchar();
37     while (!isdigit(ch))
38     {
39         if (ch == '-')
40             f = -1;
41         ch = getchar();
42     }
43     while (isdigit(ch))
44     {
45         ret = (ret << 1) + (ret << 3) + ch - '0';
46         ch = getchar();
47     }
48     ret *= f;
49     return ret;
50 }
51 template <class T>
52 inline void write(T n)
53 {
54     if (n < 0)
55     {
56         putchar('-');
57         n = -n;
58     }
59     if (n >= 10)
60     {
61         write(n / 10);
62     }
63     putchar(n % 10 + '0');
64 }
65 template <class T>
66 inline void writeln(const T &n)
67 {
68     write(n);
69     puts("");
70 }
71 int main(int argc, char const *argv[])
72 {
73 #ifndef ONLINE_JUDGE
74     freopen("in.txt", "r", stdin);
75     // freopen("out.txt", "w", stdout);
76 #endif
77     ll a, b;
78     cin >> a >> b;
79     if (a == b)
80         cout << a * 10 << " " << a * 10 + 1 << "\n";
81     else if (a == 9 && b == 1)
82         cout << 9 << " " << 10 << "\n";
83     else if (a == b - 1)
84         cout << a << " " << b << "\n";
85     else
86         cout << "-1\n";
87     return 0;
88 }
View Code

 

B. TV Subscriptions 

 

 

 

 codeforces #595 div2 pp题解

 

 找一段连续序列满足长度大于等于d且其内的不同元素最少

Solution

滑动窗口

codeforces #595 div2 pp题解
  1 #include <algorithm>
  2 #include <cctype>
  3 #include <cmath>
  4 #include <cstdio>
  5 #include <cstdlib>
  6 #include <cstring>
  7 #include <iostream>
  8 #include <map>
  9 #include <numeric>
 10 #include <queue>
 11 #include <set>
 12 #include <stack>
 13 #if __cplusplus >= 201103L
 14 #include <unordered_map>
 15 #include <unordered_set>
 16 #endif
 17 #include <vector>
 18 #define lson rt << 1, l, mid
 19 #define rson rt << 1 | 1, mid + 1, r
 20 #define LONG_LONG_MAX 9223372036854775807LL
 21 #define pblank putchar(' ')
 22 #define ll LL
 23 #define fastIO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
 24 using namespace std;
 25 typedef long long ll;
 26 typedef long double ld;
 27 typedef unsigned long long ull;
 28 typedef pair<int, int> P;
 29 int n, m, k, d;
 30 const int maxn = 1e6 + 10;
 31 template <class T>
 32 inline T read()
 33 {
 34     int f = 1;
 35     T ret = 0;
 36     char ch = getchar();
 37     while (!isdigit(ch))
 38     {
 39         if (ch == '-')
 40             f = -1;
 41         ch = getchar();
 42     }
 43     while (isdigit(ch))
 44     {
 45         ret = (ret << 1) + (ret << 3) + ch - '0';
 46         ch = getchar();
 47     }
 48     ret *= f;
 49     return ret;
 50 }
 51 template <class T>
 52 inline void write(T n)
 53 {
 54     if (n < 0)
 55     {
 56         putchar('-');
 57         n = -n;
 58     }
 59     if (n >= 10)
 60     {
 61         write(n / 10);
 62     }
 63     putchar(n % 10 + '0');
 64 }
 65 template <class T>
 66 inline void writeln(const T &n)
 67 {
 68     write(n);
 69     puts("");
 70 }
 71 unordered_map<int, int> mp;
 72 unordered_set<int> s;
 73 int a[maxn];
 74 int main(int argc, char const *argv[])
 75 {
 76 #ifndef ONLINE_JUDGE
 77     freopen("in.txt", "r", stdin);
 78     // freopen("out.txt", "w", stdout);
 79 #endif
 80     int t = read<int>();
 81     while (t--)
 82     {
 83         mp.clear();
 84         s.clear();
 85         n = read<int>(), k = read<int>(), d = read<int>();
 86         for (int i = 1; i <= n; i++)
 87             a[i] = read<int>();
 88         int head;
 89         int res = 1e9;
 90         for (int i = 1, j = 1; i <= n; i++)
 91         {
 92             head = a[i];
 93             while (j - i + 1 <= d && j <= n)
 94                 s.emplace(a[j]), ++mp[a[j]], j++;
 95             if (j - i + 1 > d)
 96                 res = min(res, (int)s.size());
 97             int t = mp[head];
 98             if (t == 1)
 99                 s.erase(head), mp.erase(head);
100             else
101                 --mp[head];
102         }
103         writeln(res);
104     }
105     return 0;
106 }
View Code

 

C. p-binary

codeforces #595 div2 pp题解

 

 定义p-二进制数如上,给定n和p,求最少的p-二进制数之和等于n

Solution

注意到如果一个数最少被表示成二进制数的数位可以贪心得到,假设为minx,那么如果当前判断的个数满足$ minx \leq x \leq newn$,其中$newn=n-x*k$

则当前x满足条件

暴力枚举

codeforces #595 div2 pp题解
  1 #include <algorithm>
  2 #include <cctype>
  3 #include <cmath>
  4 #include <cstdio>
  5 #include <cstdlib>
  6 #include <cstring>
  7 #include <iostream>
  8 #include <map>
  9 #include <numeric>
 10 #include <queue>
 11 #include <set>
 12 #include <stack>
 13 #if __cplusplus >= 201103L
 14 #include <unordered_map>
 15 #include <unordered_set>
 16 #endif
 17 #include <vector>
 18 #define lson rt << 1, l, mid
 19 #define rson rt << 1 | 1, mid + 1, r
 20 #define LONG_LONG_MAX 9223372036854775807LL
 21 #define pblank putchar(' ')
 22 #define ll LL
 23 #define fastIO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
 24 using namespace std;
 25 typedef long long ll;
 26 typedef long double ld;
 27 typedef unsigned long long ull;
 28 typedef pair<int, int> P;
 29 int n, m, k;
 30 const int maxn = 1e5 + 10;
 31 template <class T>
 32 inline T read()
 33 {
 34     int f = 1;
 35     T ret = 0;
 36     char ch = getchar();
 37     while (!isdigit(ch))
 38     {
 39         if (ch == '-')
 40             f = -1;
 41         ch = getchar();
 42     }
 43     while (isdigit(ch))
 44     {
 45         ret = (ret << 1) + (ret << 3) + ch - '0';
 46         ch = getchar();
 47     }
 48     ret *= f;
 49     return ret;
 50 }
 51 template <class T>
 52 inline void write(T n)
 53 {
 54     if (n < 0)
 55     {
 56         putchar('-');
 57         n = -n;
 58     }
 59     if (n >= 10)
 60     {
 61         write(n / 10);
 62     }
 63     putchar(n % 10 + '0');
 64 }
 65 template <class T>
 66 inline void writeln(const T &n)
 67 {
 68     write(n);
 69     puts("");
 70 }
 71 ll p[62];
 72 void init()
 73 {
 74     p[0] = 1;
 75     for (int i = 1; i < 62; i++)
 76         p[i] = 1LL << i;
 77 }
 78 int main(int argc, char const *argv[])
 79 {
 80 #ifndef ONLINE_JUDGE
 81     freopen("in.txt", "r", stdin);
 82     // freopen("out.txt", "w", stdout);
 83 #endif
 84     init();
 85     ll n = read<ll>(), k = read<ll>();
 86     ll res = -1;
 87     for (ll i = 1;; i++)
 88     {
 89         ll tmp = n - 1LL * i * k;
 90         if (tmp <= 0)
 91             break;
 92         ll t1 = 0, t2 = tmp;
 93         for (int j = 61; j >= 0 && tmp; j--)
 94             if (tmp >= p[j])
 95                 tmp -= p[j], ++t1;
 96         if (i >= t1 && i <= t2)
 97         {
 98             res = i;
 99             break;
100         }
101     }
102     writeln(res);
103     return 0;
104 }
View Code

 

D. Power Products

Description

codeforces #595 div2 pp题解

 

 给一个长为n的序列,求满足$1 \leq i \lt j \leq n,a[i]*a[j]=x^k$的数对个数

Solution

赛后A,思路来自nb群友

对于每一个数唯一分解,且指数q对k取模,那么当$q_i \gt 0$时可以得到其a[j]需要满足$q_j=k-q_i$

对于大于1e5的数据直接舍弃,注意开ll,从右往左扫一遍即为答案

codeforces #595 div2 pp题解
  1 #include <algorithm>
  2 #include <cctype>
  3 #include <cmath>
  4 #include <cstdio>
  5 #include <cstdlib>
  6 #include <cstring>
  7 #include <ext/rope>
  8 #include <iostream>
  9 #include <map>
 10 #include <numeric>
 11 #include <queue>
 12 #include <set>
 13 #include <stack>
 14 #if __cplusplus >= 201103L
 15 #include <unordered_map>
 16 #include <unordered_set>
 17 #endif
 18 #include <vector>
 19 #define lson rt << 1, l, mid
 20 #define rson rt << 1 | 1, mid + 1, r
 21 #define LONG_LONG_MAX 9223372036854775807LL
 22 #define pblank putchar(' ')
 23 #define ll LL
 24 #define fastIO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
 25 using namespace std;
 26 using namespace __gnu_cxx;
 27 typedef long long ll;
 28 typedef long double ld;
 29 typedef unsigned long long ull;
 30 typedef pair<int, int> P;
 31 int n, m, k;
 32 const int maxn = 1e5 + 10;
 33 template <class T>
 34 inline T read()
 35 {
 36     int f = 1;
 37     T ret = 0;
 38     char ch = getchar();
 39     while (!isdigit(ch))
 40     {
 41         if (ch == '-')
 42             f = -1;
 43         ch = getchar();
 44     }
 45     while (isdigit(ch))
 46     {
 47         ret = (ret << 1) + (ret << 3) + ch - '0';
 48         ch = getchar();
 49     }
 50     ret *= f;
 51     return ret;
 52 }
 53 template <class T>
 54 inline void write(T n)
 55 {
 56     if (n < 0)
 57     {
 58         putchar('-');
 59         n = -n;
 60     }
 61     if (n >= 10)
 62     {
 63         write(n / 10);
 64     }
 65     putchar(n % 10 + '0');
 66 }
 67 template <class T>
 68 inline void writeln(const T &n)
 69 {
 70     write(n);
 71     puts("");
 72 }
 73 unordered_map<int, int> mp;
 74 int prime[maxn], vis[maxn], pcnt;
 75 int a[maxn], b[maxn], c[maxn], d[maxn];
 76 void init()
 77 {
 78     for (int i = 2; i < maxn; i++)
 79     {
 80         if (!vis[i])
 81             prime[pcnt++] = i;
 82         for (int j = 0; j < pcnt && prime[j] * i < maxn; j++)
 83         {
 84             vis[i * prime[j]] = 1;
 85             if (i % prime[j] == 0)
 86                 break;
 87         }
 88     }
 89 }
 90 ll qpow(ll a, int n)
 91 {
 92     ll res = 1;
 93     while (n)
 94     {
 95         if (n & 1)
 96             res = res * a;
 97         a = a * a;
 98         n >>= 1;
 99     }
100     return res;
101 }
102 int main(int argc, char const *argv[])
103 {
104 #ifndef ONLINE_JUDGE
105     freopen("in.txt", "r", stdin);
106     // freopen("out.txt", "w", stdout);
107 #endif
108     init();
109     n = read<int>(), k = read<int>();
110     for (int i = 1; i <= n; i++)
111         a[i] = read<int>();
112     for (int i = 1; i <= n; i++)
113     {
114         if (mp.count(a[i]))
115         {
116             b[i] = b[mp[a[i]]];
117             c[i] = c[mp[a[i]]];
118             continue;
119         }
120         ll newcur = 1, tocur = 1;
121         int tmp = a[i];
122         int f = 1;
123         for (int i = 0; i < pcnt && prime[pcnt] < tmp; i++)
124             if (tmp % prime[i] == 0)
125             {
126                 int now = 0;
127                 while (tmp % prime[i] == 0)
128                 {
129                     tmp /= prime[i];
130                     ++now;
131                 }
132                 now %= k;
133                 if (now)
134                 {
135                     newcur *= qpow(prime[i], now);
136                     ll p = 1, flag = 1;
137                     for (int j = 1; j <= k - now; j++)
138                     {
139                         p = p * prime[i];
140                         if (p > 1e5)
141                         {
142                             flag = 0;
143                             break;
144                         }
145                     }
146                     if (flag && tocur * p <= 1e5)
147                         tocur *= p;
148                     else
149                         f = 0;
150                 }
151             }
152         if (tmp > 1)
153         {
154             newcur *= tmp;
155             ll p = 1, flag = 1;
156             for (int j = 1; j <= k - 1; j++)
157             {
158                 p = p * tmp;
159                 if (p > 1e5)
160                 {
161                     flag = 0;
162                     break;
163                 }
164             }
165             if (flag && tocur * p <= 1e5)
166                 tocur *= p;
167             else
168                 f = 0;
169         }
170         b[i] = newcur;
171         if (f && tocur <= 1e5)
172             c[i] = tocur;
173         else
174             c[i] = -1;
175         mp[a[i]] = i;
176     }
177     // for (int i = 1; i <= n; i++)
178     //     cout << b[i] << " " << c[i] << "\n";
179     ll res = 0;
180     for (int i = n; i >= 1; i--)
181     {
182         res += d[c[i]];
183         d[b[i]]++;
184     }
185     writeln(res);
186     return 0;
187 }
View Code

 

 

上一篇:力扣数据库题595.大的国家


下一篇:Oracle RAC环境的日志体系