Generation

generate functionSat, 21 Jan 2023

Abhas likes to play with numbers. He is given integers N and K. Find the number of triples (a, b, c) of positive integers not greater than N such that a+b, b+c, and c+a are all multiples of K. The order of a, b, and c does matter, and some of them can be the same. Input The input line contains N and K separated by space. Constraints 1≤N, K≤2×10^5 N and K are integers. Output Print the number of triples (a, b, c) of positive integers not greater than N such that a+b, b+c, and c+a are all multiples of K. Example Sample Input 1 3 2 Sample Output 1 9 Sample Input 2 5 3 Sample Output 2 1 Sample Input 3 35897 932 Sample Output 3 114191

#include <iostream> #include <unordered_set> #include <algorithm> using namespace std; int main() { int a, b; cin >> a >> b; unordered_set<int> mySet; for (int i = 1; i <= a; i++) { for (int j = 1; j <= a; j++) { for (int k = 1; k <= a; k++) { if ((i + j) % b == 0 && (j + k) % b == 0 && (k + i) % b == 0) { //cout << "(" << i << ", " << j << ", " << k << ")\n"; mySet.insert(i * 1000000 + j * 1000 + k); } } } } cout << mySet.size(); return 0; }

Questions about programming?Chat with your personal AI assistant