Problem D: Division

Problem D: Division

时间: 1ms        内存:128M

描述:

Problem D: Division

Given t, a, b positive integers not bigger than 2147483647, establish whether (t^a - 1)/(t^b -1) is an integer with less than 100 digits. Each line of input contains t, a, b. For each line of input print the formula followed by its value, or followed by "is not an integer with less than 100 digits.", whichever is appropriate.

输入:

输出:

示例输入:

2 9 3
2 3 2
21 42 7
123 911 1

示例输出:

(2^9-1)/(2^3-1) 73
(2^3-1)/(2^2-1) is not an integer with less than 100 digits.
(21^42-1)/(21^7-1) 18952884496956715554550978627384117011154680106
(123^911-1)/(123^1-1) is not an integer with less than 100 digits.

提示:

参考答案(内存最优[760]):

#include <math.h>

linepoints(double x1, double y1, double x2, double y2,
           double *a, double *b, double *c) {
   *a = y2 - y1;
   *b = x1 - x2;
   *c = *a * x1 + *b * y1;
}

bi(double x1, double y1, double x2, double y2, double *a, double *b, double *c){
   *a = 2*(x2-x1);
   *b = 2*(y2-y1);
   *c = x2*x2 + y2*y2 - x1*x1 - y1*y1;
}

isct(double a, double b, double c, double aa, double bb, double cc,
     double *x, double *y){
   double det = a*bb - b*aa;
   if (fabs(det) < 1e-10) return 0;
   *x = (-b*cc + c*bb)/det;
   *y = (a*cc - c*aa)/det;
   return 1;
}

int nd, done[200];
int i,j,k;

struct pp {
   double a, b, c;
} p[100];

double x,y,xx,yy,a,b,c;

int np, i,j,j;

char hc[20];

double xa, xb, ya, yb, area, triarea;
int nx;

main(){
   linepoints(0,0,0,10,&a,&b,&c);
   p[np].a = a; p[np].b = b; p[np++].c = c;
   linepoints(0,10,10,10,&a,&b,&c);
   p[np].a = a; p[np].b = b; p[np++].c = c;
   linepoints(10,10,10,0,&a,&b,&c);
   p[np].a = a; p[np].b = b; p[np++].c = c;
   linepoints(10,0,0,0,&a,&b,&c);
   p[np].a = a; p[np].b = b; p[np++].c = c;

   while (3 == scanf("%lf%lf %s",&xx,&yy,hc)) {
      if (strcmp(hc,"Colder")) bi(x,y,xx,yy,&p[np].a,&p[np].b,&p[np].c),np++;
      if (strcmp(hc,"Hotter")) bi(xx,yy,x,y,&p[np].a,&p[np].b,&p[np].c),np++;
      area = 0;
      nd = 0;
      for (i=0;i<np;i++) {
         nx = 0;
         for (j=0;j<np;j++) {
            if (isct(p[i].a,p[i].b,p[i].c,p[j].a,p[j].b,p[j].c,&x,&y)){
               int isin = 1;
               for (k=0;k<np;k++) {
                  if (p[k].a*x + p[k].b*y - p[k].c < -.000001) isin = 0;
               }
               if (isin) {
                  if (!nx) {
                    nx++;
                    xa = x;
                    ya = y;
                  } else if (nx<2 && (fabs(x-xa)>.000001 || fabs(y-ya)>.000001)) {
                    nx++;
                    xb = x;
                    yb = y;
                  }
               }
            }
         }
         if (nx == 2) {
            for (j=0;j<nd;j++) {
               if (fabs(p[i].a*p[done[j]].b - p[i].b*p[done[j]].a) < .000001
                 && fabs(p[i].a*p[done[j]].c - p[i].c*p[done[j]].a) < .000001
                 && fabs(p[i].b*p[done[j]].c - p[i].c*p[done[j]].b) < .000001)
                 break;
            }
            if (j == nd) {
               done[nd++] = i;
               triarea = -p[i].c / hypot(p[i].a,p[i].b) * hypot(xa-xb,ya-yb) / 2;
               area += triarea;
            }
         }
      }
      if (nd < 3 || area < .000001) area = 0;
      printf("%0.2lf\n",area);
      x = xx; y = yy;
   }
}

参考答案(时间最优[16]):

#include <math.h>

linepoints(double x1, double y1, double x2, double y2,
           double *a, double *b, double *c) {
   *a = y2 - y1;
   *b = x1 - x2;
   *c = *a * x1 + *b * y1;
}

bi(double x1, double y1, double x2, double y2, double *a, double *b, double *c){
   *a = 2*(x2-x1);
   *b = 2*(y2-y1);
   *c = x2*x2 + y2*y2 - x1*x1 - y1*y1;
}

isct(double a, double b, double c, double aa, double bb, double cc,
     double *x, double *y){
   double det = a*bb - b*aa;
   if (fabs(det) < 1e-10) return 0;
   *x = (-b*cc + c*bb)/det;
   *y = (a*cc - c*aa)/det;
   return 1;
}

int nd, done[200];
int i,j,k;

struct pp {
   double a, b, c;
} p[100];

double x,y,xx,yy,a,b,c;

int np, i,j,j;

char hc[20];

double xa, xb, ya, yb, area, triarea;
int nx;

main(){
   linepoints(0,0,0,10,&a,&b,&c);
   p[np].a = a; p[np].b = b; p[np++].c = c;
   linepoints(0,10,10,10,&a,&b,&c);
   p[np].a = a; p[np].b = b; p[np++].c = c;
   linepoints(10,10,10,0,&a,&b,&c);
   p[np].a = a; p[np].b = b; p[np++].c = c;
   linepoints(10,0,0,0,&a,&b,&c);
   p[np].a = a; p[np].b = b; p[np++].c = c;

   while (3 == scanf("%lf%lf %s",&xx,&yy,hc)) {
      if (strcmp(hc,"Colder")) bi(x,y,xx,yy,&p[np].a,&p[np].b,&p[np].c),np++;
      if (strcmp(hc,"Hotter")) bi(xx,yy,x,y,&p[np].a,&p[np].b,&p[np].c),np++;
      area = 0;
      nd = 0;
      for (i=0;i<np;i++) {
         nx = 0;
         for (j=0;j<np;j++) {
            if (isct(p[i].a,p[i].b,p[i].c,p[j].a,p[j].b,p[j].c,&x,&y)){
               int isin = 1;
               for (k=0;k<np;k++) {
                  if (p[k].a*x + p[k].b*y - p[k].c < -.000001) isin = 0;
               }
               if (isin) {
                  if (!nx) {
                    nx++;
                    xa = x;
                    ya = y;
                  } else if (nx<2 && (fabs(x-xa)>.000001 || fabs(y-ya)>.000001)) {
                    nx++;
                    xb = x;
                    yb = y;
                  }
               }
            }
         }
         if (nx == 2) {
            for (j=0;j<nd;j++) {
               if (fabs(p[i].a*p[done[j]].b - p[i].b*p[done[j]].a) < .000001
                 && fabs(p[i].a*p[done[j]].c - p[i].c*p[done[j]].a) < .000001
                 && fabs(p[i].b*p[done[j]].c - p[i].c*p[done[j]].b) < .000001)
                 break;
            }
            if (j == nd) {
               done[nd++] = i;
               triarea = -p[i].c / hypot(p[i].a,p[i].b) * hypot(xa-xb,ya-yb) / 2;
               area += triarea;
            }
         }
      }
      if (nd < 3 || area < .000001) area = 0;
      printf("%0.2lf\n",area);
      x = xx; y = yy;
   }
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注