UVa 256 Quirksome Squares (枚举||二次同余)
2013-11-23 09:08
323 查看
256 - Quirksome Squares
Time limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=192
The number 3025 has a remarkable quirk: if you split its decimal representation in two strings of equal length (30 and 25) and square the sum of the numbers so obtained, you obtain the original number:
The problem is to determine all numbers with this property having a given even number of digits.
For example, 4-digit numbers run from 0000 to 9999. Note that leading zeroes should be taken into account. This means that 0001 which is equal to
is a quirksome number of 4 digits. The number of digits may be 2,4,6 or 8. Although maxint is only 32767 and numbers of eight digits are asked for, a well-versed programmer can keep his numbers in the range of the integers. However efficiency should be given
a thought.
Input
The input of your program is a textflle containing numbers of digits (taken from 2,4,6,8), each number on a line of its own.Output
The output is a textfile consisting of lines containing the quirksome numbers (ordered according to the input numbers and for each input number in increasing order).Warning: Please note that the number of digits in the output is equal to the number in the corresponding input line : leading zeroes may not be suppressed.
Sample Input
2 2
Sample Output
00 01 81 00 01 81
直接从0~9999枚举即可,因为等式右边一定是个完全平方数。
枚举法:
/*0.015s*/ #include<bits/stdc++.h> using namespace std; const int maxn = 5; const int digit[maxn] = {0, 10, 100, 1000, 10000}; vector<int> Store[maxn]; int i, ii, j, X; void init() { for (i = 0; i < 10000; ++i) { ii = i * i; for (j = 1; j < 5; ++j) { if (i < digit[j]) { X = ii / digit[j] + ii % digit[j]; if (X == i) Store[j].push_back(ii); } } } } int main() { init(); int N, M; while (~scanf("%d", &N)) { M = N >> 1; for (i = 0; i < Store[M].size(); ++i) printf("%0*d\n", N, Store[M][i]); } return 0; }
PS:此题可抽象成一个数论的问题:(部分解法参考了这篇文章)
求解4个二元二次丢番图方程
(x+y)^2=2mx+y,m=10^k / 2 = 2^(k-1)*5^(2k),k=1,2,3,4
令x+y=t,上式化为
t^2-2mt+(2m-1)y=0(由于0<=t^2<10^2k,所以0<=t<10^k)
进而
(t-m)^2=(1-2m)y+m^2
此方程的等价于求解二次同余式
(t-m)^2 ≡ m^2 (mod (2m-1))
由于当k=1,2,3,4时,2m-1=9,99,999,9999,其素因子分解式均不含2和5,所以二次同余式必有解。
当k=1时,
同余式为
(t-5)^2 ≡ 25 (mod 9) ≡ 7 (mod 9)
由于9=3^2,同余式有2个不同余的解,
所以
t-5=±5 (mod 9)
由于0<=t<10,所以t=0,1,9,带入丢番图方程得对应的y=0,1,1
所以对应的x=0,0,8
所以2位数的quirksome number为00,01,81
当k=2时,
同余式为
(t-50)^2 ≡ 2500 (mod 99) ≡ 25 (mod 99)
由于99=3^2*11,同余式有2^2=4个不同余的解,
所以
t-50=±5 (mod 99),t-50=±50 (mod 99)
由于0<=t<100,所以t=0,1,45,55,99,带入丢番图方程得对应的y=0,1,25,25,1
所以对应的x=0,0,20,30,98
所以4位数的quirksome number为0000,0001,2025,3025,9801
当k=3时,
同余式为
(t-500)^2 ≡ 250000 (mod 999) ≡ 250 (mod 999)
由于999=3^3*37,同余式有2^2=4个不同余的解,
所以
t-500=±203 (mod 999),t-500=±500 (mod 999) (ps:27/37=(0,1,2,1,2,3))
由于0<=t<1000,所以t=0,1,297,703,999,带入丢番图方程得对应的y=0,1,209,209,1
所以对应的x=0,0,88,494,998
所以6位数的quirksome number为000000,000001,088209,494209,998001
当k=4时,
同余式为
(t-5000)^2 ≡ 25000000 (mod 9999) ≡ 2500 (mod 9999)
由于9999=3^2*11*101,同余式有2^3=8个不同余的解,
所以
t-5000=±50 (mod 9999),t-5000=±2272 (mod 9999),t-5000=±2777 (mod 9999),t-5000=±5000 (mod 9999) (ps:1111/9=(123,2,3,1),909/11=(82,1,1,1,2,1))
由于0<=t<10000,所以t=0,1,2223,2728,4950,5050,7272,7777,9999,带入丢番图方程得对应的y=0,1,1729,1984,2500,2500,1984,1729,1
所以对应的x=0,0,88,494,744,2450,2550,5288,6048,9998
所以8位数的quirksome number为00000000,00000001,04941729,07441984,24502500,25502500,52881984,60481729,99980001
****转载请注明:/article/2674307.html****
相关文章推荐
- UVa 256 - Quirksome Squares
- uva 256 - Quirksome Squares
- Uva 201 Squares (暴力 + 枚举)
- UVa 201 Squares 枚举
- UVA 201 Squares(枚举)
- uva201 Squares
- UVA11464 Even Parity (局部枚举定全局枚举)
- UVa 11205 The broken pedometer (枚举好题&巧用二进制)
- UVA 471 Magic Numbers(枚举)
- uva 1301 - Fishnet(几何+枚举)
- UVA - 140 Bandwidth(全排列枚举)
- UVa 10277 - Boastin' Red Socks (枚举)
- UVA - 201 Squares
- uva 131 The Psychic Poker Player(暴力枚举+德州扑克)
- UVA 10976 分数拆分(数论+枚举)
- 第一章例题7偶数矩阵UVa 11464(枚举)
- uva1354 枚举二叉树
- uva 1508 Equipment(暴力+枚举子集)
- UVa - 11059 Maximum Product ( 枚举区间长度 )
- UVALive 6918 And Or 数学二进制+枚举