2 条题解

  • 0
    @ 2025-11-9 16:39:59

    按题意直接模拟即可。(记得要特判 00)

    AC code:

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        freopen("sd.in", "r", stdin);
        freopen("sd.out", "w", stdout);
        int T;
        cin >> T;
        while (T--) {
            long long n;
            cin >> n;
            if (n == 0) {
                cout << "0 0" << endl;
                continue;
            }
            if (n % 4 == 2) {
                cout << "-1 -1" << endl;
                continue;
            }
            long long x, y;
            bool found = false;
            for (long long b = 1; b * b <= n; ++b) {
                if (n % b == 0) {
                    long long a = n / b;
                    if ((a + b) % 2 == 0) {
                        x = (a + b) / 2;
                        y = (a - b) / 2;
                        if (x <= n && y >= 0) {
                            cout << x << " " << y << endl;
                            found = true;
                            break;
                        }
                    }
                }
            }
            if (!found) {
                cout << "-1 -1" << endl;
            }
        }
        return 0;
    }
    
    • 0
      @ 2025-9-10 17:14:07

      众所周知:

      • 0=02−020=0^2-0^2。
      • 4k=(k+1)2−(k−1)24k=(k+1)^2-(k-1)^2。
      • 2k+1=(k+1)2−k22k+1=(k+1)^2-k^2。
      • 对于 n=4k+2n=4k+2 无解。
      • 以上式子中 kk 为非负整数。

      直接分情况讨论即可。

      #include <bits/stdc++.h>
      using namespace std;
      int main()
      {
      	freopen("sd.in", "r", stdin);
      	freopen("sd.out", "w", stdout);
      	int T;
      	cin >> T;
      	while (T--)
      	{
      		long long n;
      		cin >> n;
      		if (!n)
      		{
      			cout << "0 0" << endl;
      			continue;
      		}
      		switch (n % 4)
      		{
      			case 0:
      			{
      				cout << n / 4 + 1 << ' ' << n / 4 - 1 << endl;
      				break;
      			}
      			case 2:
      			{
      				cout << "-1 -1" << endl;
      				break;
      			}
      			default:
      			{
      				cout << n / 2 + 1 << ' ' << n / 2 << endl;
      				break;
      			}
      		}
      	}
      	return 0;
      }
      
      • 1

      [Sleeping Cup #5 / CTFPC-4th] Square Difference

      信息

      ID
      156
      时间
      1000ms
      内存
      512MiB
      难度
      2
      标签
      递交数
      52
      已通过
      12
      上传者