448E - Divisors

思路:

dfs。注意如果是1,直接返回,因为1的因子还是1。

因为x因子的因子还是x的因子,所以可以事先处理好x因子的因子在x因子中的位置。

不用这个方法也可以,用map映射vector保存因子的因子。

代码1:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem memset(a,b,sizeof(a)) const int N=1e6+;
vector<int>son[N];
vector<ll>t;
ll x;
int cnt=;
void dfs(ll k,ll n){
if(cnt==1e5)return ;
if(n==){
cout<<<<' ';
cnt++;
return ;
}
if(k==){
cout<<t[n]<<' ';
cnt++;
return ;
}else{
for(int i=;i<son[n].size();i++){
dfs(k-,son[n][i]);
if(cnt==1e5)return;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
ll k;
cin>>x>>k;
for(ll i=;i*i<=x;i++){
if(x%i==){
if(i*i==x)t.pb(i);
else t.pb(i),t.pb(x/i);
}
}
sort(t.begin(),t.end());
for(int i=;i<t.size();i++){
for(int j=;j<=i;j++)
if(t[i]%t[j]==)son[i].pb(j);
}
dfs(k,t.size()-);
return ;
}

代码2:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem memset(a,b,sizeof(a)) vector<ll>t;
unordered_map<ll,vector<ll>>vc;
ll x;
int cnt=;
void dfs(ll k,ll n){
if(cnt==1e5)return ;
if(n==){
cout<<<<' ';
cnt++;
return ;
}
if(k==){
cout<<n<<' ';
cnt++;
return ;
}
else{
if(n==x){
for(int i=;i<t.size();i++){
dfs(k-,t[i]);
}
}else{
if(vc.find(n)!=vc.end()){
for(auto x:vc[n]){
dfs(k-,x);
if(cnt==1e5)return ;
}
}else{
for(ll i=;i*i<=n;i++){
if(n%i==){
if(i*i==n)vc[n].pb(i);
else vc[n].pb(i),vc[n].pb(n/i);
}
}
sort(vc[n].begin(),vc[n].end());
for(auto x:vc[n]){
dfs(k-,x);
if(cnt==1e5)return ;
}
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
ll k;
cin>>x>>k;
for(ll i=;i*i<=x;i++){
if(x%i==){
if(i*i==x)t.pb(i);
else t.pb(i),t.pb(x/i);
}
}
sort(t.begin(),t.end());
if(k==)cout<<x<<endl;
else if(k>=1e5){
if(x==)cout<<<<endl;
else{
for(int i=;i<=1e5;i++)cout<<<<' ';
cout<<endl;
}
}
else dfs(k,x);
return ;
}

Codeforces 448E - Divisors的更多相关文章

  1. [Lyft Level 5 Challenge 2018 - Elimination Round][Codeforces 1033D. Divisors]

    题目链接:1033D - Divisors 题目大意:给定\(n\)个数\(a_i\),每个数的约数个数为3到5个,求\(\prod_{i=1}^{n}a_i\)的约数个数.其中\(1 \leq n ...

  2. [gcd]Codeforces Common Divisors

    Common Divisors time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  3. Codeforces Beta Round #85 (Div. 1 Only) B. Petya and Divisors 暴力

    B. Petya and Divisors Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/111 ...

  4. CodeForces - 27E--Number With The Given Amount Of Divisors(反素数)

    CodeForces - 27E Number With The Given Amount Of Divisors Submit Status Description Given the number ...

  5. codeforces 703E Mishka and Divisors

    codeforces 703E Mishka and Divisors 题面 给出大小为\(1000\)的数组和一个数\(k\),求长度最短的一个子序列使得子序列的元素之积是\(k\)的倍数,如果有多 ...

  6. Educational Codeforces Round 12 F. Four Divisors 求小于x的素数个数(待解决)

    F. Four Divisors 题目连接: http://www.codeforces.com/contest/665/problem/F Description If an integer a i ...

  7. Common Divisors CodeForces - 182D || kmp最小循环节

    Common Divisors CodeForces - 182D 思路:用kmp求next数组的方法求出两个字符串的最小循环节长度(http://blog.csdn.net/acraz/articl ...

  8. codeforces 111B/112D Petya and Divisors

    题目:Petya and Divisors传送门: http://codeforces.com/problemset/problem/111/B http://codeforces.com/probl ...

  9. codeforces 27E Number With The Given Amount Of Divisors

    E. Number With The Given Amount Of Divisors time limit per test 2 seconds memory limit per test 256 ...

随机推荐

  1. servlet07

    1.session验证 可以防止非登录的用户,通过在地址栏中输入地址,访问受保护的页面 step1.在用户登录成功之后,将用户的信息保存到session中 step2.在访问受保护的页面时,校验ses ...

  2. 002-ubuntu安装

    一.安装了ubuntu desktop版本后: 1.进行桥接联网. 2.运行更新:#sudo apt-get update. 3.安装net-tools网络工具包:#sudo apt install ...

  3. n是否是2的幂

    实例五:n是否是2的幂 方法一:result=n&(n-1) 如果result=0 则n是2的幂方法二:result=n&((~n)+1) 如果result=n 则n是2的幂 原数   ...

  4. Linux其他: GitBash

    git bash是Windows下的命令行工具 安装后在任何一个文件夹下右键GitBash,打开一个窗口,ssh root@xx.xxx.xxx.xx登陆到服务器,输入yes,和登陆密码可以使用敲命令 ...

  5. python webdriver操作浏览器句柄

    断言 assert self.driver.title.find(u"搜狗搜索引擎")>=0, "assert error" 浏览器后退,前进,前进前要先 ...

  6. Zookeeper学习记录(二):使用以及配置

    zookeeper已经介绍了它的原理设计以及实现方式,我们接下来介绍zookeeper的使用方法以及简单配置. 下载 获取Zookeeper的发布包,从Apache下载映像中下载一个最新稳定版本. 单 ...

  7. Linux基础命令---cp

    cp 复制文件,可以将一个文件复制到另外一个地方,也可以将多个文件复制到目录. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语 ...

  8. IEEE发布2017年编程语言排行榜:Python高居首位,java第三,php第八

    2017年7月18日,IEEE Spectrum 发布了第四届顶级编程语言交互排行榜.因为有各种不同语言的排行,所以 IEEE Spectrum 依据不同的变量对流行度进行了排行.据 IEEE Spe ...

  9. SpringBoot集成Socket服务后打包(war包)启动时如何启动Socket服务(web应用外部tomcat启动)

      1.首先知道SpringBoot打包为jar和war包是不一样的(只讨论SpringBoot环境下web应用打包)     1.1.jar和war包的打开方式不一样,虽然都依赖java环境,但是j ...

  10. 关于webpack的path和publicPath。

    path:所有输出文件的目标路径; publicPath:输出解析文件的目录,url 相对于 HTML 页面 区别:path是webpack所有文件的输出的路径,必须是绝对路径,比如:output输出 ...