UVa 256 Quirksome Squares (枚举||二次同余)

256 - Quirksome Squares

Time limit: 3.000 seconds 

http://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:

displaymath26

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 totex2html_wrap_inline28 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


****转载请注明:http://blog.csdn.net/synapse7/article/details/16886619****

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值