β ABC? (Contest)
ABC? (Contest) medium Time Limit: 2 sec Memory Limit: 128000 kB
Problem Statement :
Currently, Adk is researching a virus that has conquered the planet. For killing the virus, three chemicals (A, B, and C) are required in a particular proportion. Let the quantities of chemicals be a, b, and c respectively. The virus will be killed if a2+b2+c2=n. You need to find the number of triplets satisfying the condition for all the values of n ranging from 1 to N. Note: a, b and c must be integers. Note: Obviously, the amount of chemical cannot be negative. Input The first and only line of input contains an integer N.
Constraints: 1 <= N <= 10000 Output Output N integers, the number of triplets satisfying the condition for n = 1, 2, 3, ..., N on a new line. Example Sample Input: 10
Sample Output: 3 3 1 3 6 3 0 3 6 6
Explanation: For n=1, triplets are (0, 0, 1), (1, 0, 0), (0, 1, 0) For n=7, there are no triplets satisfying the condition. For n=10, triplets are (0, 1, 3), (0, 3, 1), (1, 0, 3), (1, 3, 0), (3, 0, 1), (3, 1, 0).
link: https://my.newtonschool.co/playground/code/m6j0jqfaa47s/
```java
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int res[] = new int[n+1];
for(int a=0; a*a<=n; a++){
for(int b=0; a*a+b*b <= n; b++){
for(int c=0;a*a+b*b+c*c<=n;c++){
int sum=a*a+b*b+c*c;
if(sum<=n){
res[sum]++;
}
}
}
}
for(int i=1; i<=n; i++){
System.out.println(res[i]);
}
}
}
```Last updated