首页 > 其他分享 >CSP-J 2023 T3 一元二次方程 解题报告

CSP-J 2023 T3 一元二次方程 解题报告

时间:2024-10-11 21:10:26浏览次数:1  
标签:cout int dfrac 2023 T3 sqrt abs 一元二次方程 Delta

CSP-J 2023 T3 一元二次方程 解题报告

Link

前言

今年\(CSP\)的原题, 回家\(1h\)内写\(AC\), 但是考场上没有写出来 , 原因是脑子太不好了, 竟然调了两个小时没有调出来. 一等奖悬那......

正题

看完题目,第一眼就是大模拟, 并且\(CCF\)绝对不会让你好受,所以出了一个如此***钻的题目, 并且要考虑到非常多的情况, 代码非常长......

最重要的一点: \(\Delta\)

\(\Delta\)是此题中最重要的分情况讨论的地方. 根据初三\(22\)章的所学知识 ,可知分三种情况:

1. \(\Delta < 0\)

不用说了
直接输出NO

2. \(\Delta = 0\)

同样的, 只有一种情况, 答案为\(- \dfrac{b}{2a}\),但是, 需要严谨的判断.

if(delta == 0) {
    if(b == 0) {
        cout << 0;
    }
    else if(a * b > 0) {
        a = abs(a);
        b = abs(b);
        cout << "-";
        if(b % (2 * a) == 0) {
            cout << b / 2 / a;
        }
        else {
            cout << b / __gcd(2 * a, b) << "/" << (2 * a) / __gcd(2 * a, b);
        }
    }
    else {
        a = abs(a);
        b = abs(b);
        if(b % (2 * a) == 0) {
            cout << b / 2 / a;
        }
        else {
            cout << b / __gcd(2 * a, b) << "/" << (2 * a) / __gcd(2 * a, b);
        }
    }
}

3. \(\Delta > 0\)

地狱.
我是分两种情况的, 一种是\(a > 0\), 一种是\(a < 0\). 这样可以分辨出是\(+ \sqrt{\Delta }\) 还是\(-\sqrt{\Delta }\)

如若\(a < 0\), 则可知答案为:

\[\dfrac{b + \sqrt{\Delta}}{-2a} \]

如若\(a > 0\), 则可知答案为:

\[\dfrac{\sqrt{\Delta} - b}{2a} \]

  • 在这里有一个技巧, 就是不论怎样, 输出时, \(\sqrt{\Delta}\)永远是正的(符号为+)

可以分两种情况:
1.第一种: 不需要写sqrt, 也就是\(\Delta\)为完全平方数时,

比较好处理, 首先需要判断\(b + \sqrt{\Delta}\)是否为\(0\). 如果是, 则直接输出\(0\); 否则输出最简分数.

其中, 一定要记住如果\((b + \sqrt{\Delta}) \% (2 * a) = 0\), 就直接输出一个整数.还要注意判断正负号.

2.第二种: 需要写sqrt, 很难.

首先, 先输出前面的内容, 也就是\(-\dfrac{b}{2a}\), 判断同上.

然后, 输出+, 代表符号.

接着, 找出三个变量, 也就是: \(\dfrac{x}{y} \sqrt{\dfrac{\Delta}{x^2}}\)中的\(x, y和\dfrac{\Delta}{x^2}\).其中,\(\sqrt{\dfrac{\Delta}{x^2}}\)为最简平方根数.

接下来是\(4\)种情况:

\(x = y\), 只有\(\sqrt{\dfrac{\Delta}{x^2}}\);

\(x \% y = 0\), 只有\(\dfrac{x}{y}\sqrt{\dfrac{\Delta}{x^2}}\)

\(y \% x = 0\), 只有\(\dfrac{\sqrt{\dfrac{\Delta}{x^2}}}{y}\)

其他情况, 输出\(\dfrac{x \times \sqrt{\dfrac{\Delta}{x^2}}}{y}\)

完结撒花!!

\(Code:\)

  • 心脏病患者请勿观看
#include <bits/stdc++.h>
using namespace std;

int T, M;
int a, b, c;

int pd(int x) {
    for(int i = sqrt(x) + 1; i >= 1; --i) {
        if(x % (i * i) == 0) {
            return i;
        }
    }
}

int main() {

    
    cin >> T >> M;

    while(T--) {
        cin >> a >> b >> c;
        
        int delta;

        delta = b * b - 4 * a * c;

        if(delta < 0) {
            cout << "NO";
        }
        else if(delta == 0) {
            if(b == 0) {
                cout << 0;
            }
            else if(a * b > 0) {
                a = abs(a);
                b = abs(b);
                cout << "-";
                if(b % (2 * a) == 0) {
                    cout << b / 2 / a;
                }
                else {
                    cout << b / __gcd(2 * a, b) << "/" << (2 * a) / __gcd(2 * a, b);
                }
            }
            else {
                a = abs(a);
                b = abs(b);
                if(b % (2 * a) == 0) {
                    cout << b / 2 / a;
                }
                else {
                    cout << b / __gcd(2 * a, b) << "/" << (2 * a) / __gcd(2 * a, b);
                }
            }
        }
        else {
            if(a < 0) {
                int mother = - 2 * a;
                int x = pd(delta);
                int y = delta / x / x;
                if(b == 0) {
                    mother = abs(mother);
                    if(y == 1) {
                        if(x == mother) {
                            cout << "1";
                        }
                        else if(x % mother == 0) {
                            cout << x / mother;
                        }
                        else {
                            cout << x / __gcd(x, mother) << "/" << mother / __gcd(x, mother);
                        }
                    }
                    else {
                        if(x == mother) {
                            cout << "sqrt(" << y << ")";
                        }
                        else if(mother % x == 0) {
                            cout << "sqrt(" << y << ")";
                            cout << "/" << mother / x;
                        }
                        else if(x % mother == 0) {
                            cout << x / mother << "*sqrt(" << y << ")";
                        }
                        else {
                            cout << x / __gcd(x, mother) << "*sqrt(" << y << ")" << "/" << mother / __gcd(x, mother);
                        }
                    }
                }
                else if(y == 1) { // 不需要sqrt
                    // 说明可以合并为同一个式子
                    int son = - b - x;
                    if(son == 0) {
                        cout << 0;
                    }
                    else if(son * mother < 0) { // 如果分子分母同号.
                        son = abs(son);
                        mother = abs(mother);
                        if(son % mother == 0) {
                            cout << son / mother;
                        }
                        else {
                            cout << son / __gcd(son, mother) << "/" << mother / __gcd(son, mother);
                        }
                    }
                    else { // 如果分子分母异号.
                        son = abs(son);
                        mother = abs(mother);
                        cout << "-";
                        if(son % mother == 0) {
                            cout << son / mother;
                        }
                        else {
                            cout << son / __gcd(son, mother) << "/" << mother / __gcd(son, mother);
                        }
                    }
                }
                else { // 需要sqrt.
                    // 1. 先输出前面的
                    if(b > 0) { // 不需要负号
                        b = abs(b);
                        mother = abs(mother);
                        if(b % mother == 0) {
                            cout << b / mother;
                        }
                        else {
                            cout << b / __gcd(b, mother) << "/" << mother / __gcd(b, mother);
                        }
                    }
                    else { // 需要负号
                        b = abs(b);
                        mother = abs(mother);
                        cout << "-";
                        if(b % mother == 0) {
                            cout << b / mother;
                        }
                        else {
                            cout << b / __gcd(b, mother) << "/" << mother / __gcd(b, mother);
                        }
                    }
                    // 2. 输出sqrt部分(不管怎样都是+)
                    cout << "+";
                    if(x == 1) { // 不需要输出前缀.
                        cout << "sqrt(" << y << ")";
                        cout << "/" << - 2 * a;
                    }
                    else {
                        if(x == mother) {
                            cout << "sqrt(" << y << ")";
                        }
                        else if(x % mother == 0) {
                            cout << x / mother << "*sqrt(" << y << ")";
                        }
                        else if(mother % x == 0) {
                            cout << "sqrt(" << y << ")";
                            cout << "/" << mother / x;
                        }
                        else {
                            cout << x / __gcd(x, mother);
                            cout << "*sqrt(" << y << ")";
                            cout << "/" << mother / __gcd(x, mother);
                        }
                    }
                }
            }
            else {
                int mother = 2 * a;
                int x = pd(delta);
                int y = delta / x / x;
                if(b == 0) {
                    mother = abs(mother);
                    if(y == 1) {
                        if(x == mother) {
                            cout << "1";
                        }
                        else if(x % mother == 0) {
                            cout << x / mother;
                        }
                        else {
                            cout << x / __gcd(x, mother) << "/" << mother / __gcd(x, mother);
                        }
                    }
                    else {
                        if(x == mother) {
                            cout << "sqrt(" << y << ")";
                        }
                        else if(mother % x == 0) {
                            cout << "sqrt(" << y << ")";
                            cout << "/" << mother / x;
                        }
                        else if(x % mother == 0) {
                            cout << x / mother << "*sqrt(" << y << ")";
                        }
                        else {
                            cout << x / __gcd(x, mother) << "*sqrt(" << y << ")" << "/" << mother / __gcd(x, mother);
                        }
                    }
                }
                else if(y == 1) { // 不需要sqrt
                    // 说明可以合并为同一个式子
                    int son = - b + x;
                    if(son == 0) {
                        cout << 0;
                    }
                    else if(son * mother > 0) { // 如果分子分母同号.
                        son = abs(son);
                        mother = abs(mother);
                        if(son % mother == 0) {
                            cout << son / mother;
                        }
                        else {
                            cout << son / __gcd(son, mother) << "/" << mother / __gcd(son, mother);
                        }
                    }
                    else { // 如果分子分母异号.
                        son = abs(son);
                        mother = abs(mother);
                        cout << "-";
                        if(son % mother == 0) {
                            cout << son / mother;
                        }
                        else {
                            cout << son / __gcd(son, mother) << "/" << mother / __gcd(son, mother);
                        }
                    }
                }
                else { // 需要sqrt.
                    // 1. 先输出前面的
                    if(b * mother < 0) { // 不需要负号
                        b = abs(b);
                        mother = abs(mother);
                        if(b % mother == 0) {
                            cout << b / mother;
                        }
                        else {
                            cout << b / __gcd(b, mother) << "/" << mother / __gcd(b, mother);
                        }
                    }
                    else { // 需要负号
                        b = abs(b);
                        mother = abs(mother);
                        cout << "-";
                        if(b % mother == 0) {
                            cout << b / mother;
                        }
                        else {
                            cout << b / __gcd(b, mother) << "/" << mother / __gcd(b, mother);
                        }
                    }
                    // 2. 输出sqrt部分(不管怎样都是+)
                    cout << "+";
                    if(x == 1) { // 不需要输出前缀.
                        cout << "sqrt(" << y << ")";
                        cout << "/" << 2 * a;
                    }
                    else {
                        mother = 2 * a;
                        if(x == mother) {
                            cout << "sqrt(" << y << ")";
                        }
                        else if(x % mother == 0) {
                            cout << x / mother << "*sqrt(" << y << ")";
                        }
                        else if(mother % x == 0) {
                            cout << "sqrt(" << y << ")";
                            cout << "/" << mother / x;
                        }
                        else {
                            cout << x / __gcd(x, mother);
                            cout << "*sqrt(" << y << ")";
                            cout << "/" << mother / __gcd(x, mother);
                        }
                    }
                }
            }
        }
        cout << endl;
    }

    return 0;
}

标签:cout,int,dfrac,2023,T3,sqrt,abs,一元二次方程,Delta
From: https://www.cnblogs.com/amlhdsan/p/18459360

相关文章

  • 跨越理论,深耕落地:2023年大模型应用实践洞察
    2023年,大模型技术在全球范围内迎来了前所未有的发展高潮,不仅在理论研究上取得了显著突破,更在实际应用中展现出了巨大的潜力和价值。科智咨询《大模型应用及落地研究(2023)》报告旨在深入分析2023年大模型在各类应用场景中的具体实践、落地成效与竞争格局,揭示大模型技术如何逐步......
  • 2023杭电多校4
    2023杭电多校4a-bProblem题目大意:每个物品都有a,ba,ba,b两个值,......
  • 2023 年和 2024 年最具威胁的 25 种安全漏洞(CWE Top 25)
    目录1.缓冲区溢出(CWE-120)2.代码注入(CWE-94)3.认证缺失(CWE-287)4.访问控制缺失(CWE-284)5.SQL注入(CWE-89)6.跨站脚本(XSS)(CWE-79)7.不安全的反序列化(CWE-502)8.脆弱的随机数生成(CWE-331)9.信息泄露(CWE-200)10.不安全的直接对象引用(CWE-63......
  • 10.10 爆零赛(2023 炼石计划 NOIP 模拟赛 #2)
    炼石计划9月10日NOIP模拟赛#2【补题】-比赛-梦熊联盟(mna.wang)模拟赛恒等式:\(0+0+0+0=0\)。复盘T1好像可做。有个显然的\(n^2\)DP。推式子的时候猜到了\(\gcd=1\)的做法。进一步尝试正解未果。T2一眼只会爆搜。想到了\(b\timesb!\)的做法,应该能过\(......
  • 【题解】2023传智杯全国大学生程序设计竞赛-初赛第一场
    A.字符串拼接直接拼接两个字符串即可,注意的是字符串可能包含空格,因此需要使用getline函数进行输入。#include<bits/stdc++.h>usingnamespacestd;intmain(){strings1,s2;getline(cin,s1);getline(cin,s2);cout<<s1+s2<<endl;return0......
  • The 2023 ICPC Asia Hangzhou Regional Contest (The 2nd Universal Cup. Stage 22: H
    The2023ICPCAsiaHangzhouRegionalContest(The2ndUniversalCup.Stage22:Hangzhou)M.V-Diagram题意:给定一个“v图”,求平均值最大的子"v图"思路:原图的最低点以及左右两个点必须取,如何先取满左边或者右边在贪心即可voidsolve(){lln;cin>>n;vect......
  • IEC104规约的秘密之七----配置参数t1,t2,t3
    104通讯前需要配置通讯参数,一般有如下参数:IP地址,端口号,k,w,t1,t2,t3,公共地址,遥控超时参数,104主规约还有一个t0参数。本次只讲解t1,t2,t3这两个参数。这三个都是超时时间,t1用于两个地方,一个是发送的I帧没有得到及时的确认,在规约文本中有如下图:B站发送I(0,0)帧后,开始计时,A站回......
  • 2023牛客OI赛前集训营-提高组(第三场) - 题解汇总
    空位与数(game)贪心即可,因为正正得正,负负也得正,所以将两个数组分别按照正负性分开,然后让正数里面大的配上大的,负数里面绝对值大的配上绝对值大的,这样可以让正积总和尽量大。剩下不足的(必须要一正一负相乘的)让绝对值大的配绝对值小的,这样可以让负积总和尽量小。#include<cstdio>#i......
  • swagger2.9.2 和 springboot3.3.4版本冲突问腿
    swagger2.9.2和springboot3.3.4版本冲突问腿问题描述:当我们使用swagger2.9.2版本的时候,如果恰好我们使用的springboot版本是3.x版本,会出现启动报错的问题解决办法:直接使用swagger3.x版本和springboot3.x版本解决步骤:1.导入swagger3.x版本的maven依赖......
  • 10.8 模拟赛(2023 CSP-S 十连测 #5)
    炼石计划10月28日CSP-S十连测#5【补题】-比赛-梦熊联盟(mna.wang)复盘T1秒了。30min。T2题目越短越难。但是链的是经典题目,写了。小样例太水,大样例太大,不方便猜结论。于是先写暴力然后自己造样例。模拟了五六组感觉可以按照lca的深度降序排序,然后能选就选。这......