• Loading...

Problem 4:Regions in n-gon

Points:

Small Case: 8 Answer: 246

Large Case: 16 Answer: 1259

Given a polygon of n vertices with each vertex pair connected by a straight line,let m be the maximum number of distinct regions inside it.

The problem is to find m mod 9997

For e.g. for n=5

ngon

number of regions = 11

Small Case: n=10

Large Case: n=11111


Solution:

For n vertices the number of regions formed are given by the relation
C(n,4) + C(n,2) - n + 1
where C(n,r) = n! / ((n-r)!*r!)

Reference:N. J. A. Sloane and Simon Plouffe, The Encyclopedia of Integer Sequences, Academic Press, 1995


Code:


#include<iostream>
#include<cmath>
using namespace std;
#define x 9997
int primes[19999]={2,3,5,7,11,13,17,19,23};

int ncr(int n,int r)
{
   int a,b,c,d,e,f,prod,temp,i;
   a=n;
   b=r;
   c=n-r;
   prod=1;
   for(i=0;primes[i]<=a;i++)
   {
      d=e=f=0;
      long long pr1=1,j;
      temp=primes[i];
      while(a/temp != 0)
      {
         d+=a/temp;
         e+=b/temp;
         f+=c/temp;
         temp=temp*primes[i];
      }
      temp=primes[i];
      for(j=1;j<=(d-e-f);j++)
         pr1=(pr1*temp)%x;
      prod=(prod*pr1)%x;
   }
   return prod;
}


int main()
{
   long long n=9,k,prod=1,i,temp,t;
   float check;
   bool flag=false;
   for(i=25;i<=11130;i+=2)
   {
      check=sqrt((double)i);
      for(k=0;primes[k]<=check;k++)
      {
         if(i%primes[k]==0)
         {
            flag=false;
            break;
         }
         else
            flag=true;             
      }
      if(flag==true)
         primes[n++]=i;          
   }
   cin>>t;
   while(t--)
   {
      int nc4,nc2,ans;
      cin>>n;
      nc4 = ncr(n,4);
      nc2 = ncr(n,2);
      ans = ((nc4+nc2-n+1)%x + x)%x;
      cout<<ans<<endl;
   }
}

Submit your solution

You need to be logged in to submit a solution.

discussions


anuragrocxPolygon
Q:I think Polygon should be regular...\\notherwise there may be different answers for same ques.
A:You have to find the maximum number of regions possible.
piratecoderspolygon
Q:is the polygon regular?
A:Not necessarily.
minikinquery
Q:how to find mod?
A:a mod b is the remainder when 'a' is divided by 'b'

post a question

Loading...

Problem 1
Problem 2
Problem 3
Problem 4
Problem 5 (bonus) Expired
Problem 1
Problem 2
Problem 3
Problem 4 (bonus) Expired
Top Scorers
rng_58 235
Anton_Lunyov 225
tourist 190
Silence 180
cedricl2 175
cgy4ever 175
DrKorbin 165
Koen 155
bbbasic 150
lunae_2 150
Submissions
Problem 1
10 pt
not submitted
98
submissions

19
correct (19 %)



25 pt
not submitted
2
submissions

1
correct (50 %)


Problem 2
5 pt
not submitted
95
submissions

38
correct (40 %)



10 pt
not submitted
19
submissions

15
correct (78 %)


Problem 3
5 pt
not submitted
153
submissions

117
correct (76 %)



15 pt
not submitted
44
submissions

25
correct (56 %)


Problem 4
10 pt
not submitted
152
submissions

54
correct (35 %)



15 pt
not submitted
23
submissions

10
correct (43 %)


Problem 5
10 pt
not submitted
36
submissions

28
correct (77 %)



20 pt
not submitted
6
submissions

2
correct (33 %)


Problem 6
5 pt
not submitted
41
submissions

29
correct (70 %)



10 pt
not submitted
15
submissions

14
correct (93 %)


Problem 7
5 pt
not submitted
38
submissions

17
correct (44 %)



10 pt
not submitted
16
submissions

15
correct (93 %)


Problem 8
5 pt
not submitted
127
submissions

104
correct (81 %)



15 pt
not submitted
47
submissions

34
correct (72 %)


Problem 9
5 pt
not submitted
86
submissions

35
correct (40 %)



15 pt
not submitted
16
submissions

15
correct (93 %)


Problem 10
5 pt
not submitted
45
submissions

23
correct (51 %)



15 pt
not submitted
15
submissions

14
correct (93 %)


Problem 11
5 pt
not submitted
81
submissions

27
correct (33 %)



15 pt
not submitted
8
submissions

3
correct (37 %)


Problem 12
5 pt
not submitted
104
submissions

88
correct (84 %)



10 pt
not submitted
31
submissions

21
correct (67 %)