时间限制:0.5s

空间限制:6M

题意:

        在b(2<b<36)进制中,找到所有长度为n(0<n<2000)的自守数k,满足k^2%b^n=k,字典序输出。

        如 十进制中     9376^2 = 87909376   后面4位数都是9376.


Solution:

               可以发现长度为n的满足要求的数中,其后n-1位也是自守数。

               这样的数是很稀疏的,可以直接从n=1的情况开始搜索。当找到长度为n的数时记录,排序输出即可。

code

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int b, n;
struct node {
int s[2009];
bool operator < (const node &A) const {
for (int i = n; i > 0; i--) {
if (A.s[i] > s[i]) return 1;
if (A.s[i] < s[i]) return 0;
}
}
} a;
vector<node> ans;
void Find (int d, int k) {
if (k == n + 1) {
if (a.s[n] != 0||n==1) ans.push_back (a);
return ;
}
for (int i = 0; i < b; i++) {
int tem = d + (i * a.s[1] << 1);
for (int i = 2; i < k; i++)
tem += a.s[i] * a.s[k - i + 1];
if ( tem % b == i) {
a.s[k] = i;
Find (tem / b, k + 1);
}
}
}
int main() {
cin >> b >> n;
for (int i = 0; i < b; i++) {
if (i * i % b == i) {
a.s[1] = i;
Find (i * i / b, 2);
}
}
sort (ans.begin(), ans.end() );
cout << (int) ans.size() << endl;
for (int i = 0; i < (int) ans.size(); i++) {
for (int j = n; j > 0; j--)
if (ans[i].s[j] <= 9)
putchar (char ('0' + ans[i].s[j]) );
else putchar ('A' + ans[i].s[j] - 10);
cout << endl;
}
return 0;
}

  

SGU 159.Self-Replicating Numbers的更多相关文章

  1. SGU 113.Nearly prime numbers

    水一个代码: #include <iostream> using namespace std; int n, a; bool ok; bool prime (int x) { ; i * ...

  2. SGU 258 Almost Lucky Numbers 接近幸运数(数位DP)

    题意: 定义一个具有2n位的正整数,其前n位之和与后n位之和相等,则为lucky数.给定一个区间,问有多少个正数可以通过修改某一位数从而变成lucky数?注意不能含前导0. 思路: 我的想法是记录那些 ...

  3. SGU 169 numbers 数学

    169.Numbers Let us call P(n) - the product of all digits of number n (in decimal notation). For exam ...

  4. Nearly prime numbers - SGU 113(素数)

    题目大意:判断一个数是否是两个素数的乘积,如果是,输出Yes,否则No. 分析:先打表求出来一部分素因子,用素数对素数判定还是比较快的. 代码如下: ========================= ...

  5. sgu 169 Numbers

    题意:n和n+1同时被数位乘积整除的k位数个数. 假如a是237,b是238.由于个位以前的数一样.那么对于2,如果a%2==0,b%2就!=0,如果a%3==0,b%3就!=0.因此个位以前的数只能 ...

  6. SGU 169 Numbers (找规律)

    题意:中文题,直接忽略... 析:先说说我的思路,我一看这个题第一感觉就是要找规律,要是挨着算,猴年马月都跑不完,更何况时间限制是0.25s,怎么找规律呢,我算了一下前10位,分别是8,1,1,3,1 ...

  7. SGU 分类

    http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...

  8. SGU-169 Numbers(找规律)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=169 解题报告: P(n)定义为n的所有位数的乘积,例如P(1243)=1*2*3* ...

  9. 59. 总结篇:数组中N(n=1,2,3)个只出现一次的数字[find N numbers which appear only once in array]

    [本文链接] http://www.cnblogs.com/hellogiser/p/find-n-numbers-which-appear-only-once-in-array.html [题目] ...

随机推荐

  1. Linux Shell编程(27)——子shell

    运行一个shell脚本时会启动另一个命令解释器. 就好像你的命令是在命令行提示下被解释的一样, 类似于批处理文件里的一系列命令.每个shell脚本有效地运行在父shell(parent shell)的 ...

  2. 【转】在VMware中安装OS X Yosemite

    原文网址:http://blog.gaohaobo.com/229.html OS X(前称:Mac OS X)操作系统是由苹果公司(Apple Inc.)为其Mac系列产品开发的.基于Unix的专属 ...

  3. HDOJ/HDU 2555 人人都能参加第30届校田径运动会了(判断加排序~)

    Problem Description 杭州师范大学第29届田径运动会圆满的闭幕了,本届运动会是我校规模最大,参赛人数最多的一次运动会.在两天半时间里,由学生.教工组成的61支代表队共2664名运动员 ...

  4. 《Linear Algebra and Its Applications》-chaper5-特征值与特征向量-基本概念

    基于之前章节的铺垫,我们这里能够很容易的引出特征向量和特征值的概念. 首先我们知道n x n矩阵的A和n维向量v的乘积会得到一个n维的向量,那么现在我们发现,经过计算u=Av,得到的向量u是和v共线的 ...

  5. codeforces 212E IT Restaurants(树形dp+背包思想)

    题目链接:http://codeforces.com/problemset/problem/212/E 题目大意:给你一个无向树,现在用两种颜色去给这颗树上的节点染色.用(a,b)表示两种颜色分别染的 ...

  6. codeforces 710C

    C. Magic Odd Square time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. C语言学习_C如何在一个文件里调用另一个源文件中的函数

    问题 C如何在一个文件里调用另一个源文件中的函数,如题. 解决办法 当程序大了代码多了之后,想模块化开发,不同文件中存一点,是很好的解决办法,那我们如何做才能让各个文件中的代码协同工作呢?我们知道,m ...

  8. [notes] ImageNet Classification with Deep Convolutional Neual Network

    Paper: ImageNet Classification with Deep Convolutional Neual Network Achievements: The model address ...

  9. [RxJS] What RxJS operators are

    We have covered the basics of what is Observable.create, and other creation functions. Now lets fina ...

  10. linux_2.6内核内存缓冲与I/O调度机制:

    http://blog.csdn.net/kaiwii/article/details/7030178 到底是BIO还是BH?答案是BIO与BH