Codeforces Round #563 (Div. 2)C

C. Ehab and a Special Coloring Problem

题目链接:http://codeforces.com/contest/1174/problem/C

题目

You‘re given an integer n. For every integer i from 2 to n, assign a positive integer ai such that the following conditions hold:

For any pair of integers (i,j), if i and j are coprime, ai≠aj

The maximal value of all ai should be minimized (that is, as small as possible).

A pair of integers is called coprime if their greatest common divisor is 1.

input

The only line contains the integer n (2≤n≤105).

output

Print n−1
integers, a2, a3, …, an (1≤ai≤n).

If there are multiple solutions, print any of them.

题意

给你一个数,让你输出长度为n-1的数组,这个数组的起始下标从2开始,使每组任意下标互质的两个数所对应的值都互质。

思路

由于范围在10^5,故可以打素数表,遇到2的倍数打印1,遇到3的倍数打印2,遇到5的倍数打印3...遇到质数的倍数打印该质数在质数表中的位置即可。

//
// Created by hjy on 19-6-4.
//

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int n;
bool prime(int m)//简单判断素数
{

    for(int i=2;i<=sqrt(m);i++)
    {
        if(m%i==0)
            return false;
    }
    return true;
}
int result[maxn]={0};
void cun()///存入表中
{
    int op=0;
    for(int i=2;i<=maxn;i++)
    {

       if(prime(i))
       {
           op++;
           //cout<<"i="<<i<<endl;
           for(int j=i;j<=maxn;j+=i)
           {
               //cout<<"j="<<j<<endl;
               result[j]=op;
               //cout<<"result[j]="<<result[j]<<endl;

           }
       }
    }
}
int main()
{

    int n;
    while(cin>>n) {
        cun();
      for(int i=2;i<=n;i++)
          cout<<result[i]<<‘ ‘;
      cout<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Vampire6/p/10990359.html

时间: 2024-08-30 17:09:55

Codeforces Round #563 (Div. 2)C的相关文章

Codeforces Round #563 (Div. 2)

CF 题目链接:https://codeforces.com/contest/1174 总结 ABC都比较水,27分钟写完了,但是D题想偏了,一直WA,AC代码确实太巧妙了. A题 #include<bits/stdc++.h> using namespace std; #define ll long long #define N 100050 int n,sum1,sum2,a[N]; template<typename T>void read(T&x) { ll k=0

Codeforces Round #563 (Div. 2)/Codeforces1174

CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i\)与\(\sum\limits_{n+1}^{2n}a_i\)差值最大,排一下序就好了 CF1174B Ehab Is an Odd Person 一个显然的性质就是如果至少有一个奇数和一个偶数,那么是可以随意调整的,也就是升序排序 否则不可以进行任何操作 CF1174C Ehab and a Special Coloring Problem 把每个质数预处理出来,\(x

Codeforces Round 563 (Div. 2) 题解

自己开了场镜像玩. 前三题大水题.D有点意思.E完全不会.F被题意杀了……然而还是不会. 不过看过(且看懂)了官方题解,所以这里是六题题解齐全的. A 水题.给原序列排序,如果此时合法则直接输出,否则说明所有数相同,一定无解. 时间复杂度 $O(n\log n)$. #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> PII; const int maxn=

Codeforces Round #563 (Div. 2) D、Ehab and the Expected XOR Problem

D. Ehab and the Expected XOR Problem Given two integers n and x, construct an array that satisfies the following conditions: for any element ai in the array, 1≤ai<2^n there is no non-empty subsegment with bitwise XOR equal to 0 or x, its length l sho

Codeforces Round #563 (Div. 2)C. Ehab and a Special Coloring Problem

原文链接:传送门思路:素数筛代码: 1 #include"iostream" 2 #include"algorithm" 3 #include"cstring" 4 using namespace std; 5 long long a[2000006],n; 6 int main(){ 7 cin>>n; 8 long long flag = 1; 9 memset(a,0,sizeof(a)); 10 for(int i=2;i&l

Codeforces Round #563 (Div. 2)B;Ehab Is an Odd Person

原文链接:任意门 题目大意:给你一组数,让你交换两个数的位置,让它们的和为奇数,且使其交换后,顺序满足最小字典序列.思路:这就是一道狗题,看代码,你就会******了,只需要sort排序.代码: 1 #include"iostream" 2 #include"algorithm" 3 #include"cstdio" 4 #include"cstring" 5 long long a[10000006],n,js=0,os=0

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我