NC221649. HamsterandEquation
描述
To proof himself is no longer a pupil, Hamster is going to solve a math problem from his brother in junior high school.
His brother is learning the quadradic equation recently, and his teacher give him a problem like this:
Find all the solution of the following equation:
where are all integers in range and is a given consistant.
Hamster doesn't know how to solve this equation due to his poor math skills, so now he turns to your help.
Can you tell him the number of valid solutions for this equation?
Note: Two Solutions are considered different if there exists an integer such that is different for two solutions.
输入描述
The test files contain one or more test cases, the first line contains a integer , representing the number of test cases in this file.
For the following lines, each line contains two integers , the constant of this equations. Here .
It is guaranteed that for all the test cases.
输出描述
For each test case, output one integer in a single line, representing the number of solutions for each given .
示例1
输入:
3 1 1 1 2 1 3
输出:
33 20 16
C++ 解法, 执行用时: 674ms, 内存消耗: 9080K, 提交时间: 2021-08-28 14:35:17
#include<iostream> #include<map> using namespace std; map<long long,int> a; int main() { int m; cin>>m; while(m--) { long long n,k,z=0; cin>>n>>k; for(int j=-n;j<=n;j++) for(int l=-n;l<=n;l++) a[j*(j+1)+l*(l+1)]++; for(int j=-n;j<=n;j++) for(int l=-n;l<=n;l++) z+=a[(j*(j+1)+l*(l+1))*k]; cout<<z<<endl; a.clear(); } return 0; }
Python3(3.9) 解法, 执行用时: 1428ms, 内存消耗: 10624K, 提交时间: 2021-05-06 10:55:29
import collections t = int(input()) for _ai in range(t): n, k = [int(i) for i in input().split()] dle = collections.defaultdict(int) ans = 0 for x1 in range(-n, n+1): for x2 in range(-n, n+1): dle[x1 * (x1 + 1) + x2 * (x2 + 1)] += 1 for p in dle: ans += dle[p] * dle.get(p*k, 0) print(ans)