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. yii2常用的migrate命令

    开发中经常会用到的方法小结: 1../yii migrate xxx_xx 在表中插入某字段 : public function up() {$this->addColumn('{{applic ...

  2. zw版【转发·台湾nvp系列Delphi例程】HALCON SmallestRectangle2

    zw版[转发·台湾nvp系列Delphi例程]HALCON SmallestRectangle2 procedure TForm1.Button1Click(Sender: TObject);var ...

  3. checkbox的readonly不起作用的解决方案

    checkbox的readonly不起作用的解决方案 <input type="checkbox" readonly /> checkbox没有readOnly属性,r ...

  4. Qt之美(一):D指针/私有实现

    The English version is available at: http://xizhizhu.blogspot.com/2010/11/beauty-of-qt-1-d-pointer-p ...

  5. vm #set、日期截取、#foreach&#if

    <div class="form_row"> <label>状态:</label> #if ($!cp.runState =='t') #set ...

  6. P3294 [SCOI2016]背单词

    P3294 [SCOI2016]背单词 Trie+贪心 倒插进树+取出重建+子树处理+贪心遍历 倒插进树:把后缀转化为前缀,所以把字符串倒着插进Trie中 取出重建:重新建立一棵以单词为节点的树,如果 ...

  7. Django 将数据库查出的 QuerySet 对象转换为 json 字符串

    通过Django查询出MySQL数据库的数据,并将查询出的QuerySet 对象转化成 json 字符串. 写这个例子的作用主要是用来为手机端提供接口用,记录一下,以后 说不准 肯定能用到! ---- ...

  8. 20145329 《网络对抗技术》Web安全基础实践

    实践的目标 理解常用网络攻击技术的基本原理.Webgoat实践下相关实验:SQL注入攻击.XSS攻击.CSRF攻击. 实验后回答问题 (1)SQL注入攻击原理,如何防御 攻击原理 SQL注入即是指we ...

  9. python创建MySQL多实例-1

    python创建MySQL多实例-1 前言 什么是多实例 多实例就是允许在同一台机器上创建另外一套不同配置文件的数据库,他们之间是相互独立的,主要有以下特点, 1> 不能同时使用一个端口 2&g ...

  10. 高通平台启动log概述(PBL log、sbl1 log、kernel log)【转】

    本文转自:https://blog.csdn.net/RadianceBlau/article/details/78416776?utm_source=blogxgwz9 高通平台启动log概述(PB ...