zoukankan      html  css  js  c++  java
  • 【codeforces 776B】Sherlock and his girlfriend

    【题目链接】:http://codeforces.com/contest/776/problem/B

    【题意】

    给你n个物品,
    第i个物品价值为i+1
    让你给这n个物品着色;
    如果a物品的价值x和b物品的价值y
    满足x是y的因子且x是质数;
    则x和y的颜色不能一样;
    求最小使用颜色数目;以及n个物品的着色方案

    【题解】

    /*
        先处理出1..10W里面哪些数字是素数;
        对于是素数的数x>=2
        x,2*x,3*x....j*x这里j*x<=n+1
        x的颜色为1,2*x,3*x....j*x颜色都为2
        两个素数肯定是可以一样的;
        然后为2的又不可能是另外一个数的素因子;
        所以可行;
        特判一下n=1,2的情况
        其实就是质数输出1不是质数就输出2就好;
        因为不是质数的数都能进行质因数分解的。
    */


    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%lld",&x)
    
    typedef pair<int, int> pii;
    typedef pair<LL, LL> pll;
    
    const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
    const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
    const double pi = acos(-1.0);
    const int N = 1e5+100;
    
    int n;
    int col[N];
    
    bool is(int x)
    {
        int len = sqrt(x);
        rep1(i, 2, len)
            if (x%i == 0)
                return false;
        return true;
    }
    
    int main()
    {
        //freopen("F:\rush.txt", "r", stdin);
        rei(n);
        if (n <= 2)
        {
            puts("1");
            rep1(i, 1, n)
            {
                printf("%d", 1);
                if (i == n)puts("");
                else
                    putchar(' ');
            }
            return 0;
        }
        puts("2");
        rep1(i, 2, n+1)
        {
            if (is(i))
                printf("1");
            else
                printf("2");
            if (i == n+1)
                puts("");
            else
                putchar(' ');
        }
        return 0;
    }
  • 相关阅读:
    RabbitMQ之六种队列模式
    面试资料
    位掩码的介绍与使用(小白鼠试毒问题)
    递归函数的写法(以strcpy函数为例)
    查找算法简介及实现
    八大排序算法概述及实现
    快速排序算法(一)
    最大(小)堆和堆排序简介
    满二叉树各种节点数目的计算
    LPSTR、LPCSTR、LPWSTR、LPCWSTR、LPTSTR、LPCTSTR的来源及意义
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626599.html
Copyright © 2011-2022 走看看