ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 A. Anagrams

A. Anagrams
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard 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.

Sample test(s)
input
3
output
1 2
input
9
output
1 2 4 8
input
33
output
1 2 4 8 16 32

 



题意:给出一个进制b,有一数字k,有某种性质。
性质:这个数x整除于k,且在b进制下长度相等与x相等的所有数都能被k整除。
求对于这个b,所有满足这个性质的数。
分析:
1、找规律,b-1的所有因数既是答案
2、证明一下。
显然不能等于b。k=b,x=k就是一个反例。
若大于b,也是不科学的。因为x=b*k是一个反例
若小于b,那么对于长度相等这一条件,可以当成原来有一个可以整除的,任意交换两个数位,仍然整除。。。

bp*b^p+bp-1*b^(p-1)+.....+bi*b^i+......+bj*b^j+......b0*b^0 = 0 (mod k) ............ 1
bp*b^p+bp-1*b^(p-1)+.....+bj*b^i+......+bi*b^j+......b0*b^0 = 0 (mod k) ............... 2
若两式都是k的倍数,可知1式-2式也是k的倍数。
则(bi * b^i + bj * b^j) - (bj * b^i + bi * b^j)是k的倍数。
(bi * b^i + bj * b^j) - (bj * b^i + bi * b^j)
= (bi - bj) * (b^i - b^j)
= (bi - bj) * b^j * (b^(i - j) - 1)
这个(b^(i - j) - 1)肯定是b-1的正整倍数。
那么,当k|b-1的时候,显然成立。
否则就是每个位相等。。。。


如果每个位相等,
k = number * (b^p+b^(p-1)+......+b^2+b^1+1)
与k是一个不大于b的正整数矛盾。不科学。


所以k必定是b-1的因数。

 1 /**
 2 Create By yzx - stupidboy
 3 */
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <cmath>
 8 #include <deque>
 9 #include <vector>
10 #include <queue>
11 #include <iostream>
12 #include <algorithm>
13 #include <map>
14 #include <set>
15 #include <ctime>
16 #include <iomanip>
17 using namespace std;
18 typedef long long LL;
19 typedef double DB;
20 #define MIT (2147483647)
21 #define INF (1000000001)
22 #define MLL (1000000000000000001LL)
23 #define sz(x) ((int) (x).size())
24 #define clr(x, y) memset(x, y, sizeof(x))
25 #define puf push_front
26 #define pub push_back
27 #define pof pop_front
28 #define pob pop_back
29 #define ft first
30 #define sd second
31 #define mk make_pair
32 
33 inline int Getint()
34 {
35     int Ret = 0;
36     char Ch = ' ';
37     bool Flag = 0;
38     while(!(Ch >= '0' && Ch <= '9'))
39     {
40         if(Ch == '-') Flag ^= 1;
41         Ch = getchar();
42     }
43     while(Ch >= '0' && Ch <= '9')
44     {
45         Ret = Ret * 10 + Ch - '0';
46         Ch = getchar();
47     }
48     return Flag ? -Ret : Ret;
49 }
50 
51 int n;
52 
53 inline void Input()
54 {
55     cin >> n;
56 }
57 
58 inline void Solve()
59 {
60     n--;
61     vector<int> ans;
62     for(int i = 1; i <= n; i++)
63     {
64         if(n / i < i) break;
65         if(n % i == 0)
66         {
67             ans.pub(i);
68             if(n / i != i) ans.pub(n / i);
69         }
70     }
71     sort(ans.begin(), ans.end());
72     int length = sz(ans);
73     for(int i = 0; i < length; i++)
74         printf(i < length - 1 ? "%d " : "%d\n", ans[i]);
75 }
76 
77 int main()
78 {
79     Input();
80     Solve();
81     return 0;
82 }
View Code

 

posted @ 2015-12-22 22:59  yanzx6  阅读(339)  评论(0编辑  收藏  举报