目录
UVA 10673 Play with Floor and Ceil
一,多元一次不定方程
CodeForces 681B Economy Game
题目:
Description
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.
Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).
Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?
Please help Kolya answer this question.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.
Output
Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).
Sample Input
Input
1359257
Output
YES
Input
17851817
Output
NO
这个题目就是暴力枚举而且,当然了,是二重循环而不是三重循环。
唯一的技巧就是,在内存循环之前,先判断了n的奇偶性,因为(56,1234)=2
加了这一句,就从30ms变成了15ms
代码:
#include<iostream>
using namespace std;
int main()
{
int n;
while (cin>>n)
{
bool flag = false;
for (int a = 0; n>=0; a++)
{
if (n % 2==0)
{
for (int b = 0; b <= n / 123456; b++)
{
if ((n - 56 * b) % 1234 == 0)
{
flag = true;
break;
}
}
}
if (flag)break;
n -= 1234567;
}
if (flag)cout << "YES";
else cout << "NO";
cout << endl;
}
return 0;
}
二,佩尔方程
1,佩尔方程(I型)
![]()
2,佩尔方程定理(I型)
(1)如果d是正整数,不是完全平方数,那么
一定有解,
如果是最小解,那么所有的解
都可以用最小解的幂求出:
(2)如果d<=0,或者d是完全平方数,那么一定有x=0或y=0
3,佩尔方程 x^2-61*y^2=1
下面我将编程求解它的最小解
如果直接枚举x和y的话,大约需要10秒
代码:
#include<iostream>
using namespace std;
int main()
{
long long x = 2, y = 0, flag = 3; //flag=x*x-61*y*y-1
while (flag)
{
if (flag > 0)
{
flag -= 61 * (2 * y + 1);
y++;
}
else
{
flag += 2 * x + 1;
x++;
}
}
cout << x << " " << y << " " << x*x;
system("pause>nul");
return 0;
}
如果先数学求解的话,自然会快一些。
首先,x必定是奇数,y必定是偶数
设x=s*2+1,y=t*2
那么s(s+1)=61t*2
分解:有2种情况
第一种,s=61*a^2,s+1=b^2,那么b^2-61*a^2=1,与x,y是最小解矛盾。
第二种,s=a^2,s+1=61*b^2,那么a^2-61*b^2=-1,化成这种佩尔方程了。
如果这个方程有解的话,原方程也有解,而且这个方程的解比原方程的解小得多。
2个方程是几乎差不多的,代码只需要略略改改就可以了。
代码:
#include<iostream>
using namespace std;
int main()
{
long long x = 2, y = 0, flag = 5;
while (flag)
{
if (flag > 0)
{
flag -= 61 * (2 * y + 1);
y++;
}
else
{
flag += 2 * x + 1;
x++;
}
}
cout << x*x * 2 + 1;
system("pause>nul");
return 0;
}
这个不需要1秒就可以算完。
最后,附上PDF的截图

4,佩尔方程(II型)

目前没有有效判断是否有解的方法。
如果是最小解,那么所有的解
都可以用最小解的幂求出:
5,力扣 2485. 找出中枢整数
给你一个正整数 n ,找出满足下述条件的 中枢整数 x :
1 和 x 之间的所有元素之和等于 x 和 n 之间所有元素之和。
返回中枢整数 x 。如果不存在中枢整数,则返回 -1 。题目保证对于给定的输入,至多存在一个中枢整数。
示例 1:
输入:n = 8
输出:6
解释:6 是中枢整数,因为 1 + 2 + 3 + 4 + 5 + 6 = 6 + 7 + 8 = 21 。
示例 2:
输入:n = 1
输出:1
解释:1 是中枢整数,因为 1 = 1 。
示例 3:
输入:n = 4
输出:-1
解释:可以证明不存在满足题目要求的整数。
提示:
1 <= n <= 1000
思路:
x^2 = (n+1)n/2
class Solution {
public:
int pivotInteger(int n) {
int x=sqrt((n+1)*n/2);
return x*x==(n+1)*n/2?x:-1;
}
};
实际上,可以求出二元不定方程 x^2 = (n+1)n/2 的所有解。
分2种情况:
(1)n=2a^2, n+1=b^2, x=ab
b^2-2a^2=1
I型佩尔方程,最小解b=3,a=2,所有解可以表示成b+ac = (3+2c)^k , c=sqrt(2), k=1,2,3,4....
(2)n=a^2,n+1=2b^2, x=ab
a^2-2b^2=-1
II型佩尔方程,最小解a=1,b=1,所有解可以表示成a+bc =(1+c)^k,c=sqrt(2), k=1,3,5,7....
利用组合数学继续求解:
(1)n=((3+2c)^2k + (3-2c)^2k - 2)/4 , k=1,2,3,4....
(2)n=((3+2c)^k + (3-2c)^k - 2)/4 , k=1,3,5,7....
综上,n的所有解是n=((3+2c)^k + (3-2c)^k - 2)/4 , k=1,2,3,4....
回到题目本身,枚举n的前几个解:
int main()
{
double c = sqrt(2);
double d1 = 3 + c * 2, d2 = 3 - c * 2;
double p1 = 1, p2 = 1;
cout.setf(ios::fixed);
for (int i = 0; i < 20; i++) {
p1 *= d1, p2 *= d2;
cout << (p1 + p2 - 2) / 4 << ",";
}
return 0;
}
输出:
1.000000,8.000000,49.000000,288.000000,1681.000000,9800.000000,57121.000000,332928.000000,1940449.000000,11309768.000000,65918161.000000,384199200.000000,2239277040.999999,13051463047.999994,76069501248.999969,443365544447.999817,2584123765440.999023,15061377048199.994141,87784138523760.968750,511643454094367.812500
验证了确实都是整数之后,只需要打印整数部分:
1,8,49,288,1681,9800,57121,332928,1940449,11309768,65918161,384199200,-2147483648,-2147483648,-2147483648,-2147483648,-2147483648,-2147483648,-2147483648,-2147483648,
二次编码
class Solution {
public:
int pivotInteger(int n) {
map<int,int>m;
m[1] = 1, m[8] = 6, m[49] = 35, m[288] = 204;
return m[n]?m[n]:-1;
}
};
验证过,果然AC了。
三,勾股数、幂和方程
1,勾股数
(1)勾股数的通项公式:2uvd、(u^2-v^2)d、(u^2+v^2)d,其中(u,v)=1且u,v一奇一偶
(2)若x^2、y^2、z^2成等差数列,
不定方程及其应用详解

本文探讨了多元一次不定方程的暴力枚举解法,佩尔方程的理论及其应用,包括如何求解x^2-61*y^2=1的最小解,并介绍了其他相关数学问题,如勾股数、幂和方程的性质。此外,还涉及编程挑战,如最小好进制的寻找和解决实际数学题目的实例。
最低0.47元/天 解锁文章
5448





