首页 > 其他分享 >Codeforces Round 949 (Div. 2)D. Turtle and Multiplication(欧拉路径、线性筛、思维构造)

Codeforces Round 949 (Div. 2)D. Turtle and Multiplication(欧拉路径、线性筛、思维构造)

时间:2024-06-07 17:12:05浏览次数:13  
标签:Turtle std 949 int Codeforces vector minp primes

Problem - D - Codeforces

 

 

按照官方正解做即可,顺带存个jiangly板子。

 1 #include <bits/stdc++.h>
 2 
 3 using i64 = long long;
 4 std::vector<int> minp, primes;
 5 
 6 void sieve(int n) {
 7     minp.assign(n + 1, 0);
 8     primes.clear();
 9     
10     for (int i = 2; i <= n; i++) {
11         if (minp[i] == 0) {
12             minp[i] = i;
13             primes.push_back(i);
14         }
15         
16         for (auto p : primes) {
17             if (i * p > n) {
18                 break;
19             }
20             minp[i * p] = p;
21             if (p == minp[i]) {
22                 break;
23             }
24         }
25     }
26 }
27 
28 void solve() {
29     int n;
30     std::cin >> n;
31     
32     int m = 1;
33     while (n - 1 > (m % 2 == 1 ? m * (m + 1) / 2 : m * m / 2 + 1)) {
34         m++;
35     }
36     
37     std::vector<int> a;
38     a.reserve(n);
39     
40     std::vector g(m, std::vector<int>(m, 1));
41     std::vector<int> cur(m);
42     if (m % 2 == 0) {
43         for (int i = 1; i < m - 1; i += 2) {
44             g[i][i + 1] = g[i + 1][i] = 0;
45         }
46     }
47     
48     auto dfs = [&](auto &&self, int x) -> void {
49         for (int &i = cur[x]; i < m; i++) {
50             if (g[x][i]) {
51                 g[x][i] = g[i][x] = 0;
52                 self(self, i);
53             }
54         }
55         a.push_back(primes[x]);
56     };
57     dfs(dfs, 0);
58     
59     a.resize(n);
60     for (int i = 0; i < n; i++) {
61         std::cout << a[i] << " \n"[i == n - 1];
62     }
63 }
64 
65 int main() {
66     std::ios::sync_with_stdio(false);
67     std::cin.tie(nullptr);
68     
69     sieve(3E5);
70     
71     int t;
72     std::cin >> t;
73     
74     while (t--) {
75         solve();
76     }
77     
78     return 0;
79 }

 

       

标签:Turtle,std,949,int,Codeforces,vector,minp,primes
From: https://www.cnblogs.com/ccsu-kid/p/18237533

相关文章

  • Codeforces Round 950 (Div. 3) A B C D E
    A.ProblemGeneratortimelimitpertest:1secondmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputVladisplanningtoholdm......
  • Codeforces Round 951 (Div. 2)
    A.GuesstheMaximum题意:给定一个数组,求一个k值,k满足对于任意的这个数组的区间的最大值max,k<max。求满足条件的最大k。思路:只考虑长度为2的区间即可。参与到比较中的数值一定是两个数中的大数,从所有大数中选出最小的一个即可。总结:赛时很快就A掉了,但是思考的不够细节,思维太......
  • codeforces round 961题解(A、B、C)
    A.GuesstheMaximum因为\(i<j\),所以所有的\([i,j]\)区间中都至少包含两个相邻元素,所以只要求出所有相邻元素中较大值的最小值即可。intn;inta[N];voidsolve(){cin>>n;intmin_v=1e9+1;for(inti=1;i<=n;i++){cin>>a[i];......
  • CodeForces Round #951(Div. 2) 补题记录(A~D)
    A容易发现对于任意一个长度为\(n\),下标从\(1\)开始的序列\(a\),若\(1\lel\ler<n\),则必然有\(\max\limits_{i=l}^ra_i\le\max\limits_{i=l}^{r+1}a_i\)。若\(1<l\ler\len\),则必然有\(\max\limits_{i=l}^ra_i\le\max\limits_{i=l-1}^ra_i\)。很显然Bob希望......
  • codeforces 1442 D Codeforces Round 681 (Div. 1, based on VK Cup 2019-2020 - Fina
    链接大意就是给你n组物品,这n组物品里面每组有\(t_i\)个,且他们是按照价值不降的顺序排列的。现在允许取k个物品,每个物品必须取在数组的开头处,每个物品在被取用后就会消失。问你最大能够拿到多少价值的物品。其中\(n,k\leq1500,\sumt_i\leq1e6,a_i\leq1e8\)很背包吧。可......
  • Codeforces Round 949题解(A、B、C)
    A.TurtleandPiggyArePlayingaGame首先\(p\)选\(2\)的话除得最慢,得的分多。考虑二进制表示,如果\(x=(1000000000)_{bin}\),则每次除以\(2\)都是相当于右移一位,除完之后仍然是\(2\)的倍数,变成\(1\)的步数就是把最高位的\(1\)移动到\(0\)位的步数。因为\(2l\ler\),所以\(l......
  • Codeforces Round 950 (Div. 3)个人题解(A~F1)
    CodeforcesRound950(Div.3)个人题解(A~F1)题解火车头#define_CRT_SECURE_NO_WARNINGS1#include<iostream>#include<vector>#include<algorithm>#include<set>#include<unordered_map>#include<cstring>#include<cstdio&......
  • Codeforces Round 949 (Div. 2) 中文题解
    A对于一个特定的\(x\),Piggy总是会选择\(p\)使得\(p\)是质数,所以分数就是\(x\)的质因子个数。容易发现至少有\(t\)个质因子的数是\(2^t\)。最大的满足\(2^t\ler\)的整数\(t\)是\(\left\lfloor\log_2r\right\rfloor\)。又因为\(2l\ler\),所以\(\log_2l+......
  • 【数据分享】中国民政统计年鉴(1949-2022)
    大家好!今天我要向大家介绍一份重要的中国民政统计数据资源——《中国民政统计年鉴》。这份年鉴涵盖了从1949年到2022年中国民政统计全面数据,并提供限时免费下载。(无需分享朋友圈即可获取)数据介绍从1949年到2022年,每年的《中国民政统计年鉴》不仅是数据记录的集合,更是我国社会......
  • Codeforces Round 950 (Div. 3)
    https://codeforces.com/contest/1980A.ProblemGenerator题意:Thereisgoingtobemroundsnextmouth,eachofthemonthshouldbeconsistof"ABCDEFG",countthenumebrofalphabetweshouldaddtosatisfythisrequirementunderagivingsequenc......