bzoj3629[JLOI2014]聪明的燕姿

http://www.lydsy.com/JudgeOnline/problem.php?id=3629

搜索。

我们知道:

如果$N=\prod\limits_{i=1}^{m}p_{i}^{k_{i}}$,其中$p_{i}$为质数,那么N的约数和为$\prod\limits_{i=1}^{m}(p_{i}^{0}+p_{i}^{2}+...+p_{i}^{k_{i}})$

如$36=2^{2}*3^{2}$,那么$36$的约数和为$(2^{0}+2^{1}+2^{2})*(3^{0}+3^{1}+3^{2})=91$

我们搜索找到所有合法最小的$p_{i}$和它次数$k_{i}$,然后DFS进入下一次搜索中。

如果发现当前的约数和为一个质数+1,我们可以加到答案去。

觉得答案的个数很少。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj
 
using namespace std;

typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP;

#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b)  for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define p_b(a) push_back(a)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))

template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}

const DB EPS=1e-9;
inline int sgn(DB x){if(abs(x)<EPS)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

inline int gint()
  {
        int res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }
inline LL gll()
  {
      LL res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }

const int maxN=100000;

int N=maxN;
int flag[maxN+100];
int cnt,p[maxN+100];
int S;

int ge,out[maxN+1000];

inline int isprime(int v)
  {
      if(v<=N)return !flag[v];
      int i;for(i=1;i<=cnt && p[i]*p[i]<=v;i++)if(v%p[i]==0)return 0;
      return 1;
  }

inline void DFS(int last,int now,int tot)
  {
      if(tot==1){out[++ge]=now;return;}
      if(isprime(tot-1) && tot-1>p[last])
          out[++ge]=now*(tot-1);
        for(int i=last+1;i<=cnt;i++)
          for(int j=p[i],k=p[i]+1;k<=tot;j=j*p[i],k+=j)
            if(tot%k==0)
              DFS(i,now*j,tot/k);
  }

int main()
  {
      freopen("bzoj3629.in","r",stdin);
      freopen("bzoj3629.out","w",stdout);
      int i,j;
      flag[1]=1;
      re(i,2,N)
        {
            if(!flag[i])p[++cnt]=i;
            for(j=1;j<=cnt && i*p[j]<=N;j++)
              {
                  flag[i*p[j]]=1;
                  if(i%p[j]==0)break;
              }
        }
      while(SF("%d\n",&S)!=EOF)
        {
            ge=0;
            DFS(0,1,S);
            sort(out+1,out+ge+1);
            ge=unique(out+1,out+ge+1)-out-1;
            PF("%d\n",ge);
            re(i,1,ge)PF("%d%c",out[i],i==ge?'\n':' ');
        }
      return 0;
  }
View Code

 

posted @ 2015-08-27 15:37  maijing  阅读(280)  评论(0编辑  收藏  举报