E. 因数串(EOJ Monthly 2020.7 Sponsored by TuSimple)

比赛连接https://acm.ecnu.edu.cn/contest/292/problem/E/

 

E. 因数串


相邻两位相差一个质因子,即相邻两位有且仅有一个质因子的幂次相差1,其余质因子个的幂次相等。

采用类似格雷码的方式构造即可


 

#include<iostream>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
int n;
ll now=1,p[15],k[15];
bool flag[15];
void dfs(int x){
    if(x==n+1) return;
    if(!flag[x]){
        dfs(x+1);
        for(int i=1;i<=k[x];i++){
            now*=p[x];
            cout<<now<<endl;
            dfs(x+1);
        }
    }else{
        dfs(x+1);
        for(int i=1;i<\=k[x];i++){
            now/=p[x];
            cout<<now<<endl;
            dfs(x+1);
        }
    }
    flag[x]^=1;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>p[i]>>k[i];
    }
    cout<<now<<endl;
    dfs(1);
}

 

posted @ 2020-07-20 10:09  xuanzo  阅读(143)  评论(0编辑  收藏  举报