zoukankan      html  css  js  c++  java
  • BZOJ 1053

    一个写过的题还搞了半个多小时。。羞耻MAX。。

    求反素数的核心思想是“用大的(质数)不如用小的”。一个数的约数个数等于其各质因子的次数加1后的乘积,因此可以从小到大考虑每个质数,枚举其次数,DFS之即可。具体细节详见代码。

    // BZOJ 1053
    
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
     typedef long long LL;
     const int prime[16]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53}, INF=0x7fffffff;
    
     #define rep(i,a,b) for (int i=a; i<=b; i++)
     #define read(x) scanf("%d", &x)
    
     LL ans;
     int n, tot;
    
     void DFS(int dep, int limit, LL num, int cnt) {  
     // dep为第几个质数,limit为当前最大次数,num为当前的数,cnt为num的约数个数
     	if (dep>16 || num>n) return;
     	if ((num<ans && cnt==tot) || cnt>tot) ans=num, tot=cnt;
     	rep(i,1,limit) {  // 次数直接从1开始搜(注意求反素数的核心思想)
     	    num*=prime[dep];
     	    if (num>n) break;
     	    DFS(dep+1, i, num, cnt*(i+1));
     	}
     }
    
    int main()
    {
    	read(n);
    
    	ans=INF; tot=1;  // ans为答案,tot为约数个数
    	DFS(0,32,1,1);  // 注意数组下标从0开始!
    	
    	printf("%lld
    ", ans);
    
    	return 0;
    }
    


  • 相关阅读:
    阿里云服务器 API 的使用
    CMDB 资产管理
    Django uwsgi+nginx+django 部署上线
    Django Middleware 中间件
    Django Form 表单
    Django FBV and CBV
    Django cookie and session
    Django 分页器
    Django ORM
    CodeVS 1008 选数(DFS)
  • 原文地址:https://www.cnblogs.com/yearwhk/p/5119866.html
Copyright © 2011-2022 走看看