CSU1619 Con + tin / (ued + Frac / tions)

来源:互联网 发布:魔方淘宝 编辑:程序博客网 时间:2024/06/02 15:20

题目链接:

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=170517

题意:

给两个连分式,分别求他们的和差积余

思路:

应该是模拟水题,当天状态实在差得可以。化成分子分母形式就好,中间有个判断条件出错所以导致溢出。

源码:

错误版:

#include <cstdio>

#include <cmath>

#include <cstring>

#include <string>

#include <algorithm>

#include <iostream>

#include <queue>

using namespace std;

#define gcd(a,b) __gcd(a,b)

typedef long long ll;

int const MAXN = 10+5;

int data[MAXN];

int n,m;

ll mot1,mot2,son1,son2;

//ll gcd(ll a,ll b)

//{

//   if(a%b==0)

//       return b;

//   return gcd(b,a%b);

//}

void data_input()

{

   for(int i=0; i<n; i++)

       scanf("%d",&data[i]);

   if(n>1){

       son1 = data[n-1]*data[n-2] + 1;

       mot1 = data[n-1];

       for(int i =n-3; i>=0; i--){

           ll t1 = son1;ll t2 = mot1;

           son1 = data[i]*t1 + t2;

           mot1 = t1;

       }

    }

   else{

       son1 = data[0];

       mot1 = 1;

    }

   for(int i=0; i<m; i++)

       scanf("%d",&data[i]);

   if(m>1){

       son2 = data[m-1]*data[m-2] + 1;

       mot2 = data[m-1];

       for(int i =m-3; i>=0; i--){

           ll t1 = son2;ll t2 = mot2;

           son2 = data[i]*t1 + t2;

           mot2 = t1;

       }

    }

   else{

       son2 = data[0];

       mot2 = 1;

    }

//   printf("son1 = %I64d,son2 = %I64d,mot1 = %I64d,mot2 =%I64d\n",son1,son2,mot1,mot2);

}

void cal(ll son,ll mot)

{

//   if((son<0 && mot>0) || (son>0 && mot<0)){

     if(son * mot < 0){

       if(son%mot==0){

//            printf("first\n");

//            printf("son = %lld, mot =%lld\n",son,mot);

           printf("%lld\n",son/mot);

           return;

       }

       else{

//            printf("second\n");

           printf("%lld ",son/mot-1);

           cal(mot,mot+son%mot);

       }

    }

   else{

       printf("%lld",son/mot);

       if(son%mot==0){

//            puts("third\n");

           printf("\n");

           return;

       }

       else {

//            puts("forth\n");

           printf(" ");

           cal(mot,son%mot);

       }

    }

}

int main()

{

   int _Ca = 0;

   while(scanf("%d%d",&n,&m)!=EOF && n+m){

       printf("Case %d:\n", ++_Ca);

       data_input();

       ll t1,t2;

       ll g = gcd(max(mot1,mot2),min(mot1,mot2));

//       printf("g = %I64d\n",g);

       t1 = son1*(mot2/g);

       t2 = son2*(mot1/g);

//       printf("t1 = %I64d,t2 = %I64d,t1+t2 = %I64d,mother is%I64d\n",t1,t2,t1+t2,mot1*mot2/g);

//       cout<<t1+t2<<endl;

       cal(t1+t2,mot1*mot2/g);

       cal(t1-t2,mot1*mot2/g);

       cal(son1*son2,mot1*mot2);

//       printf("\n");

       cal(son1*mot2,son2*mot1);

    }

   return 0;

}

正确版:

#include <cstdio>

#include <cmath>

#include <cstring>

#include <string>

#include <algorithm>

#include <iostream>

#include <queue>

using namespace std;

#define gcd(a,b) __gcd(a,b)

typedef long long ll;

int const MAXN = 10+5;

int data[MAXN];

int n,m;

ll mot1,mot2,son1,son2;

//ll gcd(ll a,ll b)

//{

//   if(a%b==0)

//       return b;

//   return gcd(b,a%b);

//}

void data_input()

{

   for(int i=0; i<n; i++)

       scanf("%d",&data[i]);

   if(n>1){

       son1 = data[n-1]*data[n-2] + 1;

       mot1 = data[n-1];

       for(int i =n-3; i>=0; i--){

           ll t1 = son1;ll t2 = mot1;

           son1 = data[i]*t1 + t2;

           mot1 = t1;

       }

    }

   else{

       son1 = data[0];

       mot1 = 1;

    }

   for(int i=0; i<m; i++)

       scanf("%d",&data[i]);

   if(m>1){

       son2 = data[m-1]*data[m-2] + 1;

       mot2 = data[m-1];

       for(int i =m-3; i>=0; i--){

           ll t1 = son2;ll t2 = mot2;

           son2 = data[i]*t1 + t2;

           mot2 = t1;

       }

    }

   else{

       son2 = data[0];

       mot2 = 1;

    }

//   printf("son1 = %I64d,son2 = %I64d,mot1 = %I64d,mot2 =%I64d\n",son1,son2,mot1,mot2);

}

void cal(ll son,ll mot)

{

   if((son<0 && mot>0) || (son>0 && mot<0)){

       if(son%mot==0){

//            printf("first\n");

//            printf("son = %lld, mot =%lld\n",son,mot);

           printf("%lld\n",son/mot);

           return;

       }

       else{

//           printf("second\n");

           printf("%lld ",son/mot-1);

           cal(mot,mot+son%mot);

       }

    }

   else{

       printf("%lld",son/mot);

       if(son%mot==0){

//            puts("third\n");

           printf("\n");

           return;

        }

       else {

//            puts("forth\n");

           printf(" ");

           cal(mot,son%mot);

       }

    }

}

int main()

{

   int _Ca = 0;

   while(scanf("%d%d",&n,&m)!=EOF && n+m){

       printf("Case %d:\n", ++_Ca);

       data_input();

       ll t1,t2;

       ll g = gcd(max(mot1,mot2),min(mot1,mot2));

//       printf("g = %I64d\n",g);

       t1 = son1*(mot2/g);

       t2 = son2*(mot1/g);

//       printf("t1 = %I64d,t2 = %I64d,t1+t2 = %I64d,mother is%I64d\n",t1,t2,t1+t2,mot1*mot2/g);

//       cout<<t1+t2<<endl;

       cal(t1+t2,mot1*mot2/g);

       cal(t1-t2,mot1*mot2/g);

       cal(son1*son2,mot1*mot2);

//       printf("\n");

       cal(son1*mot2,son2*mot1);

    }

   return 0;

}

0 0