Algorithm


Problem link- https://www.spoj.com/problems/CRDS/

CRDS - Cards

no tags 

 

Maricruz have a lot of cards, she always uses her cards to build pyramids as shown in the following image:

Cards

A pyramid card of 3 levels. She always wonder how many cards does she need to make a pyramid card of N levels. Your task is to answer that question.

Input

The first line of the input contains an integer 1 <= T <= 1,000. Each of the following T lines will have an integer 1 <= N <= 1,000,000.

Output

For each case, output a single line consisting of the number of cards needed to build a pyramid card of level N modulo 1,000,007.

Example

Input Example
2
3
7

Output Example
15
77

 

Code Examples

#1 Code Example with Python Programming

Code - Python Programming

T = int(input())
for _ in range(T):
    N = int(input())
    print(N * (3 * N + 1) / 2 % 1000007)
Copy The Code & Try With Live Editor

Input

x
+
cmd
2
3
7

Output

x
+
cmd
15
77

#2 Code Example with Java Programming

Code - Java Programming

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 * SPOJ Problem: Cards(CRDS)
 * This one was all about deriving the formula. Also it is equally important that all calculations are done on 'long' vars.
 * @author madhurbhargava
 */
public class CRDS {

	
	public static void main(String[] arguments) throws Exception
	{
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out = new PrintWriter(System.out);
		int numTestCases = Integer.parseInt(reader.readLine());
		
		//it is imp that this is long
		long modNum = 1000007;
		
		for(int i = 0; i < numTestCases; i++)
		{
			//it is imp that all calculations are done on long
			long numLevels = Long.parseLong(reader.readLine());
			long cards = 3*((numLevels*(numLevels+1))/2) - numLevels;
			cards = cards % modNum;
			out.println(cards);
			out.flush();
			
		}
	}
	
	
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
2
3
7

Output

x
+
cmd
15
77

#3 Code Example with C++ Programming

Code - C++ Programming

#include <iostream>
using namespace std;

int main() {
	int t,l,sum=0,i;
	cin>>t;
	while(t--){
		cin>>l;
		sum=l*2;
		for(i=l-1;i>0;i--){
			sum+=i*2+i;
		}
		cout<<sum<<endl;
		
	}
	return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
2
3
7

Output

x
+
cmd
15
77
Advertisements

Demonstration


SPOJ Solution-Cards-Solution in C, C++, Java, Python

Previous
SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python