【CodeForces 577C】 唯一分解定理

Vasya and Petya are playing a simple game. Vasya thought of number x between 1 and n, and Petya tries to guess the number.

Petya can ask questions like: “Is the unknown number divisible by number y?”.

The game is played by the following rules: first Petya asks all the questions that interest him (also, he can ask no questions), and then Vasya responds to each question with a ‘yes’ or a ‘no’. After receiving all the answers Petya should determine the number that Vasya thought of.

Unfortunately, Petya is not familiar with the number theory. Help him find the minimum number of questions he should ask to make a guaranteed guess of Vasya’s number, and the numbers yi, he should ask the questions about.

Input
A single line contains number n (1 ≤ n ≤ 103).

Output
Print the length of the sequence of questions k (0 ≤ k ≤ n), followed by k numbers — the questions yi (1 ≤ yi ≤ n).

If there are several correct sequences of questions of the minimum length, you are allowed to print any of them.

Examples
Input
4
Output
3
2 4 3
Input
6
Output
4
2 4 3 5
Note
The sequence from the answer to the first sample test is actually correct.

If the unknown number is not divisible by one of the sequence numbers, it is equal to 1.

If the unknown number is divisible by 4, it is 4.

If the unknown number is divisible by 3, then the unknown number is 3.

Otherwise, it is equal to 2. Therefore, the sequence of questions allows you to guess the unknown number. It can be shown that there is no correct sequence of questions of length 2 or shorter.

題意:問至少有多少因子才能確定1->n之間的每個數(具體操作見題面)

思路:

一開始的想法就是將當前遍歷到的數分解成質因數相乘的形式,這樣由於質因數都是比當前i小且肯定計入答案的,這樣就可以充分利用。
比如 6 = 2x3 ,2和3都計算過了,所以通過2和3可以確定6。
但是像18 = 2x32的形式如何處理呢?它分解出來的因子是和上面的6一樣的。這種情況下我們發現只要多加進來一個32就行了,而這個32在前面遍歷到9的時候一定計入答案中了。
所以我們只需要將所有素數及其冪次計入答案即可。到這裏用線性篩也行,不過我順着思路就用唯一分解定理寫了。

AC代碼:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e6+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll dp[1005];
ll vis[1005];
ll res[1005];

int main()
{
    ll n;
    while(cin>>n)
    {
        mem(vis,0); ll p = 0;
        rep(i,1,n)
        {
            if(i==1) {vis[1]=1; continue;}
            ll m = sqrt(i*1.0);
            ll tot = 0; ll t = i;
            rep(j,2,m)
            {
                if(t%j==0)
                {
                    dp[tot++] = j;
                    while(t%j==0) t/=j;
                }
            }
            if(t>1) dp[tot++] = t;
            if(tot==1)
            {
                res[p++] = i;
                vis[i] = 1;
                continue;
            }
        }
        cout<<p<<endl;
        rep(i,0,p-1) cout<<res[i]<<' ';cout<<endl;
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章