2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest A

A. Anagrams
time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
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,求所有k,使得任意被k整除的b进制数经过重排后仍然能被k整除。
以b=9为例:b进制数可表示为a0x9^0+a1x9^1+a2x9^2+…,显然满足对于任意n>=0,9^n%k==1的k均为解,等价于k|(9^n-1),等价于k|(9-1),(因为1+9+9^2+…9^n-1随n变化,显然互素)。将k|(9-1)的9替换成b即可。所以原题即为求b-1的所有因子。

贴一下旗神的代码:

// ml:run = $bin < input
#include <iostream>
#include <set>

using ll = long long;
ll b;

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin >> b;
    b--;
    std::set<int> ans;
    ans.insert(1);
    ans.insert(b);
    for (ll i = 2; i * i <= b; i++)
        if (b % i == 0) {
            ans.insert(i);
            ans.insert(b / i);
        }
    for (auto i : ans) std::cout << i << " ";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值