Special Triplets CodeChef Solution – Queslers

Problem: Special Triplets CodeChef Solution

Gintoki has been very lazy recently and he hasn’t made enough money to pay the rent this month. So the old landlady has given him a problem to solve instead, if he can solve this problem the rent will be waived. The problem is as follows:

A triplet of integers (A,B,C)(A,B,C) is considered to be special if it satisfies the following properties for a given integer NN :

  • AmodB=CAmodB=C
  • BmodC=0BmodC=0
  • 1≤A,B,C≤N1≤A,B,C≤N

Example: There are three special triplets for N=3N=3.

  • (1,3,1)(1,3,1) is a special triplet, since (1mod3)=1(1mod3)=1 and (3mod1)=0(3mod1)=0.
  • (1,2,1)(1,2,1) is a special triplet, since (1mod2)=1(1mod2)=1 and (2mod1)=0(2mod1)=0.
  • (3,2,1)(3,2,1) is a special triplet, since (3mod2)=1(3mod2)=1 and (2mod1)=0(2mod1)=0.

The landlady gives Gintoki an integer NN. Now Gintoki needs to find the number of special triplets. Can you help him to find the answer?

Input Format

  • The first line of the input contains a single integer TT denoting the number of test cases. The description of TT test cases follows.
  • The first and only line of each test case contains a single integer NN.

Output Format

  • For each testcase, output in a single line the number of special triplets.

Constraints

  • 1≤T≤101≤T≤10
  • 2≤N≤1052≤N≤105

Subtasks

Subtask #1 (5 points): 2≤N≤1032≤N≤103

Subtask #2 (20 points): 2≤N≤1042≤N≤104

Subtask #3 (75 points): Original constraints

Sample Input 1 

3
3
4
5

Sample Output 1 

3
6
9

Explanation

Test case 11: It is explained in the problem statement.

Test case 22: The special triplets are (1,3,1)(1,3,1), (1,2,1)(1,2,1), (3,2,1)(3,2,1), (1,4,1)(1,4,1), (4,3,1)(4,3,1), (2,4,2)(2,4,2). Hence the answer is 66.

Test case 33: The special triplets are (1,3,1)(1,3,1), (1,2,1)(1,2,1), (3,2,1)(3,2,1), (1,4,1)(1,4,1), (4,3,1)(4,3,1), (1,5,1)(1,5,1), (5,4,1)(5,4,1), (5,2,1)(5,2,1), (2,4,2)(2,4,2). Hence the answer is 99.

Special Triplets CodeChef Solution Using Python

# cook your dish here
t = int(input())

for i in range(t):
    n = int(input())
    c = 0
    for j in range(1, n // 2 + 1):
        for k in range(2 * j, n + 1, j):
            a = n // k
            b = n % k
            if b >= j:
                c += a + 1
            else:
                c += a
    print(c)

Special Triplets CodeChef Solution Using C++

#include <bits/stdc++.h>
using namespace std;

#define ll long long

ll m = 1e5+5;
vector<vector<ll>>v(m);

void preprocess(){
    for(ll i = 1;i<m;i++){
        for(ll j = 2*i;j<m;j+=i){
            v[j].push_back(i);
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	// your code goes here
	preprocess();
	ll t;
	cin >> t;
	while(t--){
	    ll n;
	    cin >> n;
	    ll ans = 0;
	    for(ll i = 2;i<=n;i++){
	        ll fact = v[i].size();
	        ll last = n-(n%i);
	        ans += fact*(last/i);
	        for(ll j = 0;j<fact;j++){
	            if(last+v[i][j] <= n)
	                ans++;
	        }
	    }
	    cout << ans << "\n";
	}
	return 0;
}

Special Triplets CodeChef Solution Using Java

import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main {
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;
 
        public FastReader()
        {
            br = new BufferedReader(
                new InputStreamReader(System.in));
        }
 
        String next()
        {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
 
        int nextInt() { return Integer.parseInt(next()); }
 
        long nextLong() { return Long.parseLong(next()); }
 
        double nextDouble()
        {
            return Double.parseDouble(next());
        }
 
        String nextLine()
        {
            String str = "";
            try {
                str = br.readLine();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
  public static void main (String[] args) throws java.lang.Exception
  {
    FastReader sc = new FastReader();
    int t = sc.nextInt();
    while(t-- > 0) 
    {
        int n = sc.nextInt();
        int cnt = 0;
        for(int i = 1; i <= n; i++) 
        {
            for(int j = i; j <= n; j += i) 
            {
                if(j % i == 0) 
                {
                    for(int k = i; k <= n; k += j) 
                    {
                        if(k % j == i) 
                        cnt++;
                    }
                }
            }
        }
        System.out.println(cnt);
    }
  }
}
Special Triplets CodeChef Solution Review:

In our experience, we suggest you solve this Special Triplets CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Special Triplets Problem is available on Hacker Rank for Free, if you are stuck anywhere between a compilation, just visit Queslers to get Special Triplets CodeChef Solution.

Conclusion:

I hope this Special Triplets CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More on Queslers >>

CodeChef Solution

Cognitive Class Answers

Leetcode Solution

Coursera Quiz Answers

Leave a Reply

Your email address will not be published. Required fields are marked *