CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • February CookOff
    • February Long Contest
    • January CookOff
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » October 2009 (Contest IX) » Congruent triangles

Congruent triangles

Problem code: H3

  • All Submissions

All submissions for this problem are available.

After learning about congruent triangles in his recent mathematics lesson, Johnny has become very excited. He has even invented an interesting counting problem involving congruent triangles!

Recall that two triangles are congruent if their corresponding sides are equal in length and their corresponding angles are equal in size. A lattice triangle is a triangle such that the coordinates of all its vertices are integers. Johnny's problem can now be described as follows:

You are given an integer M and a lattice triangle ABC all of whose vertices (A, B, and C) are inside rectangle RM. RM is the rectangle having (0,0) as its bottom-left corner and (M,M) as its top-right corner. In other words, 0 ≤  xA, yA, xB, yB, xC, yC ≤ M. The problem is to count the number of lattice triangles congruent to ABC also having all their vertices inside the rectangle RM.

Could you help Johnny write a program to solve this problem?

Input

The first line contains t, the number of test cases (about 10). Then t test cases follow. Each test case has the following form:

  • The first line contains two numbers M and K (1≤ M ≤ 1000, 1≤ K ≤1000). K is the number of given triangles.
  • Each line in the next K lines contains 6 integers xA, yA, xB, yB, xC, yC (0 ≤ xA, yA, xB, yB, xC, yC≤ M) describing a triangle ABC. It is guaranteed that ABC is always a triangle (i.e., non-degenerate).

The input for successive test cases is separated by a blank line.

Output

For each test case, output a line containing the string "Case #T:" where T should be replaced by the corresponding test case number.

Then, K lines should follow, each line containing the number of triangles having vertices inside the rectangle RM, congruent to the corresponding triangle given in the input.

Print a blank line after each test case.

Example

Input:
2

2 2
0 0 0 2 2 0
0 0 1 1 2 0

3 2
0 0 0 2 2 0
0 0 1 1 2 0

Output:
Case #1:
4
8

Case #2:
16
24

Output details

The 8 triangles congruent to the second triangle (in the first test case) are presented in the following figure:


Date: 2009-09-15
Time limit: 1

s
Source limit: 50000
Languages: C C99 strict C++ PAS gpc PAS fpc JAVA NICE JAR C# C#2 NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp SCALA HASK CAML CLPS PRLG WSPC BF ICK TEXT


  • Submit

Comments

  • Login or Register to post a comment.

Can I get more test cases for

seedrick @ 2 Oct 2009 05:40 PM
Can I get more test cases for this problem? I've cross checked my result using brute force, I dont know why it always says "Wrong answer"

Currently it is not possible

admin @ 2 Oct 2009 05:42 PM

Currently it is not possible to get more test cases as the contest is on.

Should we print an empty line

thocevar @ 2 Oct 2009 06:40 PM

Should we print an empty line between tests cases or also after last test case?

Print a blank line after

admin @ 2 Oct 2009 06:59 PM

Print a blank line after every test case.

wont you be thinking that the

nitin3star @ 3 Oct 2009 01:37 AM

wont you be thinking that the second triangle (in the first test case) should have more congruent triangles.. as you have not considered most of its congruent triangles.

1. angle B at each of the corners of the rectangles.

 

They must be lattice

triplem @ 3 Oct 2009 02:50 AM

They must be lattice triangles; the ones you mentioned are not.

In the first triangle of

ayushman @ 3 Oct 2009 08:35 AM

In the first triangle of second test case shouldn't there be 8 triangles as the sideways triangle would overlap with the top down triangles.If not can u tell me how the 16 are formed

The problem doesn't say

triplem @ 3 Oct 2009 10:13 AM

The problem doesn't say anything about triangles overlapping.

@Nitin Youre right that would

shashankp @ 3 Oct 2009 03:13 PM
@Nitin Youre right that would make more cases.. @Stephen Consider B at corners... the triangles will still be lattice...

Hi, i am getting compilation

guptaanil2k1 @ 3 Oct 2009 08:17 PM

Hi,

i am getting compilation error for my submissions. Is there anyway I can find out what are compilation errors.

Anil

@ Shashank - no it won't. The

triplem @ 4 Oct 2009 02:55 AM

@ Shashank - no it won't. The distance from A to B isn't an integer.

Is a mirror image of a

seedrick @ 5 Oct 2009 03:17 AM
Is a mirror image of a triangle considered to be congruent? for example: is [(0,0),(1,0),(2,2)] congruent with [(2,2),(3,0),(4,0)]?

I think the description and

triplem @ 5 Oct 2009 05:42 AM

I think the description and in particular the pictures at the bottom are pretty clear about that..

Hello, for the first test

udayabhanug @ 5 Oct 2009 11:37 AM
Hello, for the first test case's first sub-case, involving (0,0),(0,2),(2,0) vertices, I am getting 8 congruent triangles, not 4, in a 2*2 box.

Then you are incorrect.

triplem @ 5 Oct 2009 12:37 PM

Then you are incorrect.

my code is showing run time

marutinaidu @ 5 Oct 2009 11:49 PM

my code is showing run time eror

how can i know eror code

or can i get the input on which it is tested so that even i can test

is (0,0),(0,1),(2,0) and

szk @ 8 Oct 2009 09:31 PM
is (0,0),(0,1),(2,0) and (0,0),(2,0),(2,1) also considered congruent??

@Shahzor - Yes.

innocentboy @ 8 Oct 2009 11:18 PM

@Shahzor - Yes.

package

abhinava.srivastava @ 23 Oct 2009 01:58 PM

package com.abhi.triangles;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

public class CongruentCalculator {
public static void main(String[] args) throws IOException {

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);

int numerOfTestCases = Integer.parseInt(in.readLine());

for (int n = 0; n < numerOfTestCases; n++) {
out.println("Case #" + (n + 1) + ":");
in.readLine();
String s = in.readLine();
int idx = s.indexOf(' ');
int m = Integer.parseInt(s.substring(0, idx));
int K = Integer.parseInt(s.substring(idx + 1));
int N = (m + 1) * (m + 1);
for (int k = 0; k < K; k++) {
StringTokenizer st = new StringTokenizer(in.readLine());
int x1 = Integer.parseInt(st.nextToken());
int y1 = Integer.parseInt(st.nextToken());
int x2 = Integer.parseInt(st.nextToken());
int y2 = Integer.parseInt(st.nextToken());
int x3 = Integer.parseInt(st.nextToken());
int y3 = Integer.parseInt(st.nextToken());
int counter = 0;

ThreePointSet triangle = getThreePointsSet(x1, x2, x3, y1, y2,
y3, m);

for (int p = 1; p <= N - 2; p++) {
for (int q = p + 1; q <= N - 1; q++) {
for (int r = q + 1; r <= N; r++) {
ThreePointSet threePointSet = new ThreePointSet(p,
q, r, m);
if (triangle.isEqual(threePointSet)) {
counter += 1;
}

}

}

}
out.println(counter);
}
out.println();
}
out.close();

}

public static ThreePointSet getThreePointsSet(int x1, int x2, int x3,
int y1, int y2, int y3, int m) {
int d1 = ThreePointSet.getSquaredDistance(x1, y1, x2, y2);
int d2 = ThreePointSet.getSquaredDistance(x1, y1, x3, y3);
int d3 = ThreePointSet.getSquaredDistance(x2, y2, x3, y3);
Set<Integer> distanceSet = new HashSet<Integer>();
distanceSet.add(d1);
distanceSet.add(d2);
distanceSet.add(d3);

return new ThreePointSet(getPoint(x1, y1, m), getPoint(x2, y2, m),
getPoint(x3, y3, m), m, distanceSet);
}

public static int getPoint(int x1, int y1, int m) {
return (m + 1) * x1 + y1 + 1;
}
}

 

package com.abhi.triangles;
import java.util.HashSet;
import java.util.Set;

public class ThreePointSet {
int p1;
int p2;
int p3;
int boundary;   
Set<Integer> distanceSet;

ThreePointSet(int p1, int p2, int p3, int boundary, Set<Integer> distanceSet) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.boundary = boundary;
this.distanceSet = distanceSet;

}

ThreePointSet(int p1, int p2, int p3, int boundary) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.boundary = boundary;
}

public boolean isEqual(Object o) {
ThreePointSet otherPointsSet = (ThreePointSet) o;
Set<Integer> otherPointDistanceSet = getDistanceSet(otherPointsSet);

return distanceSet.equals(otherPointDistanceSet);
}

public int getX(int point) {
if (point % (boundary + 1) != 0) {
return point / (boundary + 1);
} else
return (point / (boundary + 1)) - 1;
}

public int getY(int point) {
if (point % (boundary + 1) != 0) {
return (point % (boundary + 1)) - 1;
} else
return boundary;
}

Set<Integer> getDistanceSet(ThreePointSet points) {
int p1 = points.p1;
int p2 = points.p2;
int p3 = points.p3;

int x1 = getX(p1);
int x2 = getX(p2);
int x3 = getX(p3);
int y1 = getY(p1);
int y2 = getY(p2);
int y3 = getY(p3);

int d1 = getSquaredDistance(x1, y1, x2, y2);
int d2 = getSquaredDistance(x1, y1, x3, y3);
int d3 = getSquaredDistance(x2, y2, x3, y3);

Set<Integer> distanceSet = new HashSet<Integer>();
distanceSet.add(d1);
distanceSet.add(d2);
distanceSet.add(d3);

return distanceSet;
}

public static int getSquaredDistance(int x1, int y1, int x2, int y2) {
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}

}

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our judge accepts solutions in over 35+ programming languages. Online programming was never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming competitions that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com