zoukankan      html  css  js  c++  java
  • CF 577C Vasya and Petya's Game

    题意:一个游戏,A童鞋在1~n的范围里猜一个数,B童鞋询问一个集合,A童鞋要对集合里每个数做出回答,他猜的数能否给整除,B要通过这些答案得到A猜的数,最少需要猜哪些数?

    解法:一个数可以由若干个质数的指数次幂相乘得到,所以只要询问小于n的所有质数的指数次幂就可以得到全部数的答案。

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<limits.h>
    #include<time.h>
    #include<stdlib.h>
    #include<map>
    #include<queue>
    #include<set>
    #include<stack>
    #include<vector>
    #define LL long long
    
    using namespace std;
    
    vector <int> prime;
    void init()
    {
        bool isprime[1005] = {0};
        for(int i = 2; i <= 1000; i++)
        {
            if(!isprime[i])
            {
                prime.push_back(i);
                for(int j = i + i; j <= 1000; j += i) isprime[j] = 1;
            }
        }
    }
    int main()
    {
        init();
        int n;
        while(~scanf("%d", &n))
        {
            vector <int> ans;
            for(int i = 0; i < prime.size(); i++)
            {
                int tmp = prime[i];
                while(tmp <= n)
                {
                    ans.push_back(tmp);
                    tmp *= prime[i];
                }
            }
            cout << ans.size() << endl;
            for(int i = 0; i < ans.size(); i++)
            {
                if(i) printf(" ");
                cout << ans[i];
            }
            puts("");
        }
        return 0;
    }
    

      

  • 相关阅读:
    378. Kth Smallest Element in a Sorted Matrix
    372. Super Pow
    357. Count Numbers with Unique Digits
    345. Reverse Vowels of a String
    343. Integer Break
    347. Top K Frequent Elements
    344. Reverse String
    338. Counting Bits
    326. Power of Three
    python练习
  • 原文地址:https://www.cnblogs.com/Apro/p/4802171.html
Copyright © 2011-2022 走看看