P1586 Solution to the Square Theorem

P1586 Square Theorem

The meaning of the question : The meaning of the question is very simple.
Insert picture description here
Solution : I heard that this problem can be quadruple cycle + pruning, the time complexity is one card, the positive solution is dp, dp is a good method , use recursion to consider, dp[j][k] means that j uses k square numbers The number of schemes represented, the transfer equation is dp [j] [k] = ∑ dp [j − i ∗ i] [k − 1] dp[j][k]=\sum dp[ji*i][k-1 ]dp[j][k]=dp[jii][k1 ] , Zhengxu can also use the idea of ​​a complete backpack.Nine lessons about backpacks (manually funny).

Code :

#include<bits/stdc++.h>
using namespace std;
const int N = 32768;
const int M = 4e4+10;
const int Mod = 1e9+7;
int dp[M][5];
int main() {
    
    
	int t, n;
	dp[0][0] = 1;
	for(int i = 1; i*i <= N; ++i) {
    
     //枚举平方数 
		for(int j = i*i; j <= N; ++j) {
    
     //递推!!! 
			for(int k = 1; k <= 4; ++k) {
    
      //表示选取了多少个平方数 
				dp[j][k] += dp[j-i*i][k-1]; 
			}
		}
	}
	scanf("%d", &t);
	while(t--) {
    
    
		scanf("%d", &n);
		int ans = 0;
		for(int i = 1; i <= 4; ++i) {
    
    
			ans += dp[n][i];
		}
		printf("%d\n", ans);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_43408978/article/details/109058059