Anagrams(思维)

Consider the positional numeral system with a given base b. A positive integer x is called b-anagram of a positive integer y if they have the same length of representation in this system (without leading zeroes) and y can be obtained by rearranging the digits of x.

A positive integer k is called b-stable if for every integer m that is divisible by k all its b-anagrams are also divisible by k. Your task is to find all b-stable integers k for a given base b.

Input

The only line of the input contains an integer b — the base of the given positional numeral system (2 ≤ b ≤ 2·109).

Output

Print all b-stable integers k represented in the standard decimal numeral system. They must be printed in ascending order.

Examples

Input
3
Output
1 2
Input
9
Output
1 2 4 8
Input
33
Output
1 2 4 8 16 32

思路

分析一下可以知道答案就是输出b-1的所有因数,直接暴力输出会超时,用优先队列再加一些基本的优化就可以ac

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<iomanip>
#include<unordered_map>
using namespace std;
typedef long long ll;
priority_queue<ll,vector<ll>,greater<ll> > ans;
bool flag;
void print()
{
    if(flag)
        cout<<" ";
    flag=true;
}
int main()
{
    ios::sync_with_stdio(false);
    ll n;
    cin>>n;
    n--;
    flag=false;
    for(int i=1; i<=sqrt(n); i++)
    {
        if(n%i==0)
        {
            ans.push(i);
            if(n/i!=i)
                ans.push(n/i);
        }
    }
    while(!ans.empty())
    {
        print();
        cout<<ans.top();
        ans.pop();
    }
    cout<<endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值