【JLOI2014】【BZOJ3629】聰明的燕姿,jloi2014bzoj3629

來源:互聯網
上載者:User

【JLOI2014】【BZOJ3629】聰明的燕姿,jloi2014bzoj3629

Description

陰天傍晚車窗外
未來有一個人在等待
向左向右向前看
愛要拐幾個彎才來
我遇見誰會有怎樣的對白
我等的人他在多遠的未來
我聽見風來自地鐵和人海
我排著隊拿著愛的號碼牌
城市中人們總是拿著號碼牌,不停尋找,不斷匹配,可是誰也不知道自己等的那個人是誰。可是燕姿不一樣,燕姿知道自己等的人是誰,因為燕姿數學學得好!燕姿發現了一個神奇的演算法:假設自己的號碼牌上寫著數字S,那麼自己等的人手上的號碼牌數位所有正約數之和必定等於S。
所以燕姿總是拿著號碼牌在地鐵和人海找數字(喂!這樣真的靠譜嗎)可是她忙著唱《綠光》,想拜託你寫一個程式能夠快速地找到所有自己等的人。
Input

輸入包含k組資料(k<=100)
對於每組資料,輸入包含一個號碼牌S
Output

對於每組資料,輸出有兩行,第一行包含一個整數m,表示有m個等的人,第二行包含相應的m個數,表示所有等的人的號碼牌。注意:你輸出的號碼牌必須按照升序排列。
Sample Input

42

Sample Output

3

20 26 41

HINT

對於100%的資料,有S<=2*109

Source

使用約數和定理.
約數和定理

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define MAXN 100010#define LL long longusing namespace std;int n;bool not_prime[MAXN];int prime[MAXN],num;int ans[MAXN],top;void check_prime(){    for (int i=2;i<MAXN;i++)    {        if (!not_prime[i])  prime[++num]=i;        for (int j=1;j<=num&&i*prime[j]<MAXN;j++)        {            not_prime[i*prime[j]]=1;            if (i%prime[j]==0)  break;        }    }}bool Prime(int x){    if (x<MAXN) return (!not_prime[x]);    else     {        int t=sqrt(x);        for (int i=1;prime[i]<=t;i++)            if  (x%prime[i]==0) return 0;        return 1;    }}void dfs(int now=1,int x=1,int left=n){    if (left==1)    {        ans[++top]=now;return;    }    if (left-1>=prime[x]&&Prime(left-1))    ans[++top]=(left-1)*now;    for (int i=x;prime[i]*prime[i]<=left;i++)    {        LL sum=prime[i]+1,tmp=prime[i];        for (;sum<=n;tmp*=prime[i],sum+=tmp)            if (left%sum==0)    dfs(now*tmp,i+1,left/sum);    }}int main(){    check_prime();    while (~scanf("%d",&n))    {        top=0;        memset(ans,0,sizeof(ans));        dfs();        printf("%d\n",top);        if (top==0) continue;        sort(ans+1,ans+top+1);        for(int i=1;i<top;i++)            printf("%d ",ans[i]);        if  (top)   printf("%d\n",ans[top]);    }}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.