2 条题解

  • 0
    @ 2023-11-1 18:20:47

    写这种时间不敏感的大模拟最关键的就是多写注释,然后一切尽量按照原题的意思、流程去做。

    #include <bits/stdc++.h>
    using namespace std;
    int T, M;
    int gcd (int a, int b)
    {
        if(b == 0)
            return a;
        return gcd(b, a % b);
    }
    void outPQ(int p, int q)
    {
        //输出 p / q
        //预处理1:约分
        int d = gcd(p, q);
        if (d < 0)
            d *= -1;
        p /= d;
        q /= d;
        //输出
        if (q == 1)
            cout << p;
        else
            cout << p << "/" << q;
    }
    int main()
    {
        freopen("uqe.in", "r", stdin);
        freopen("uqe.out", "w", stdout);
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> T >> M;
        while (T--)
        {
            int a, b, c;
            cin >> a >> b >> c;
            //分母变成正数
            if (a < 0)
            {
                a *= -1;
                b *= -1;
                c *= -1;
            }
            int delta = b * b - 4 * a * c;
            // 无解
            if (delta < 0)
            {
                cout << "NO\n";
                continue;
            }
            //有解时处理输出较大者 (-b+sqrt(delta)) / 2a
            // (p1 + p2 * sqrt(p3)) / q
            int q = 2 * a;
            int p1 = -b;
            int p2 = 1;
            int p3 = delta;
            //化简 p3
            for(int i = 2; i * i <= p3; i++)
            {
                int ii = i * i;
                while (p3 % ii == 0)
                {
                    p3 /= ii;
                    p2 *= i;
                }
            }
            if(p3 == 0)
                p2 = 0;
            // 解为有理数
            if (p3 == 1 || p2 == 0)
            {
                outPQ(p1 + p2, q);
                cout << "\n";
                continue;
            }
            // 解为 x = q1 + q2 sqrt(r)
            // 使用 pair 储存有理数
            pair<int,int> q1 = make_pair(p1, q);
            pair<int,int> q2 = make_pair(p2, q);
            int r = p3;
            if (q1.first != 0)
            {
                //q1 != 0
                outPQ(q1.first, q1.second);
                cout<<"+";
            }
            if (q2.first == q2.second)
            {
                //q2 == 1
                cout << "sqrt(" << r << ")";
            }
            else if (q2.first % q2.second == 0)
            {
                //q2 为整数
                cout << q2.first / q2.second << "*sqrt(" << r << ")";
            }
            else if (q2.second % q2.first == 0)
            {
                //1/q2 为整数
                cout << "sqrt(" << r << ")/" << q2.second / q2.first;
            }
            else
            {
                //化简 q2
                int d = gcd(q2.first, q2.second);
                q2.first /= d;
                q2.second /= d;
                cout << q2.first << "*sqrt(" << r << ")/" << q2.second;
            }
            cout << "\n";
        }  
        return 0;
    }
    
    
    • 0
      @ 2023-10-28 20:34:38
      #include<bits/stdc++.h>
      using namespace std ;
      long long t,m,a,b,c,de;
      long long gcd(long long x,long long y)
      {
      	if(y==0)return x;
      	else return gcd(y,x%y);
      }
      void doit(long long fz,long long fm)
      {
      	long long gcdd=abs(gcd(fz,fm));
      	fz/=gcdd;fm/=gcdd;
      	if(fm==1)cout<<fz;
      	else
      	cout<<fz<<"/"<<fm;
      }
      int main()
      {
      	freopen("uqe.in","r",stdin);
      	freopen("uqe.out","w",stdout); 
      	cin>>t>>m;
      	while(t--)
      	{
      		cin>>a>>b>>c;
      		de=b*b-4*a*c;
      		if(de<0)cout<<"NO";
      		else
      		{
      			long long zs=0;
      			for(long long i=1;i*i<=de;i++)
      			{
      				if(de%(i*i)==0)zs=i; 
      			}
      			if(zs*zs==de)
      			{
      				if(a>0)
      				{
      					long long fz=-b+zs,fm=2*a;
      					doit(fz,fm);
      				}
      				else
      				{
      					long long fz=b+zs,fm=-2*a;
      					doit(fz,fm);
      				}
      			}
      			else
      			{
      				long long fz,fm;
      				if(a>0)
      				fz=-b,fm=2*a;
      				else fz=b,fm=-2*a;
      				if(fz!=0)
      				{
      					doit(fz,fm);
      					cout<<"+";
      				}
      				long long r=de/(zs*zs);
      				if(a>0)
      				fz=zs,fm=2*a;
      				else fz=zs,fm=-2*a;
      				long long gcdd=gcd(fz,fm);
      				fz/=gcdd,fm/=gcdd;
      				if(fm==1)
      				{
      					if(fz==1)cout<<"sqrt("<<r<<")";
      					else cout<<fz<<"*sqrt("<<r<<")";
      				}
      				else if(fz==1)cout<<"sqrt("<<r<<")/"<<fm;
      				else cout<<fz<<"*sqrt("<<r<<")/"<<fm;
      	
      			}
      		}
      		cout<<endl;
      	}
      	return 0;
      }
      
      • 1

      信息

      ID
      1349
      时间
      1000ms
      内存
      512MiB
      难度
      8
      标签
      递交数
      17
      已通过
      6
      上传者