在SPOJ AP3中答错答案(AP -完成系列)

Getting Wrong Answer in SPOJ AP3 (AP - Complete The Series)?

本文关键字:AP 系列 答案 AP3 SPOJ      更新时间:2023-10-16

我遇到了一个关于SPOJ的问题,在这里

我做了数学计算,并最终编码了解决方案。

我在ideone上得到了正确的答案,但是SPOJ拒绝我的答案为"WA"。

#include <iostream>
#include <cmath>
typedef long long LL;
using namespace std;
int main() {
        LL t,x,y,sum,d,n,cd,a,i;
        cin>>t;
    while(t--)
    {
        cin >> x >> y >> sum;
        d = 5*y + 7*x + 2*sum;
        n = (d + sqrt(d*d - 48*sum*(x+y)))/(2*(x+y));
        cd = ( y - x )/(n-6);
        a = x - 2 * cd;
        cout<<n<<endl;
        for(i=0;i<n;i++) {
            cout<< a + cd * i<<" ";
        }
        cout<<endl;
    }
    return 0;
}
输入:

3

3 7 55

8 11 77

9 17 120

输出:

10

1 2 3 4 5 6 7 8 9 10

7

2 5 8 11 14 17 20

8

1 5 9 13 17 21 25 29

我哪里搞错了。我认为问题可能是精度,但我无法检查。我是错误的数据类型或使用的精度或一些i/O优化需要吗?输入[5 1 25]是否有效?因为它给出的输出为[7 6 5 4 3 2 1 0 -1 -2]

希望我不是太迟,有一些precision loss,像你必须首先在long double中获取你的答案,然后使用llrint()函数将其转换为long long int类型,我直到现在还没有解决这个问题,因为我的数学计算没有产生正确的结果,我使用你的代码只是为了检查结果并得到AC。下面是代码

#include <iostream>
#include <cmath>
typedef long long int LL;
using namespace std;
int main() {
        LL t,x,y,sum,i,a,tointeger,newdiff;
        long double n,cd,d;
        cin>>t;
        while(t--) {
            cin >> x >> y >> sum;
            d = 5.0 * y + 7.0 * x + 2.0 * sum;
            n = (d + sqrt(d * d - 48.0 * sum * (x + y))) / (2.0 * (x + y));
            tointeger = llrint(n);
            cd = (y - x) / (tointeger - 6.0);
            newdiff = llrint(cd);
            a = x - 2 * newdiff;
            cout<<tointeger<<endl;
            for(i=0;i<tointeger;i++) {
                cout<< a + newdiff * i<<" ";
            }
            cout<<endl;
    }
    return 0;
}