一个关于 LL 与 int 的问题
查看原帖
一个关于 LL 与 int 的问题
664034
_xxxxx_楼主2024/12/28 21:42

为什么

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, p, q;
inline int read()
{
	int s = 0, w = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9'){ if(ch == '-'){w = -1;} ch = getchar();}
	while(ch >= '0' && ch <= '9'){s = (s << 3) + (s << 1) + (ch ^ 48); ch = getchar();}
	return s * w;
}
inline void write(int x)
{
    if(x < 0)
	{
        putchar('-');
        x = -x;
    }
    if(x > 9)
		write(x / 10);
    putchar(x % 10 + '0');
}
void solve()
{
    n = read(), p = read(), q = read();
    if(p == 0) return cout << 0 << endl, void();
    // if(q == 0) return cout << (n & 1 ? p : 0) << endl, void();
    if(n == 1) return cout << p << endl, void();
    int pos = (q / p - 1) / 2;
    // cout << pos << endl;
    int num = (n - pos) / 2 * q;
    // cout << num << endl;
    int ans = (p * (1 + pos) * pos / 2) + ((n - pos) & 1 ? num + (pos + 1) * p : num);
    // write(ans);
    // printf("\n");
    printf("%lld\n", ans);
}
signed main()
{
    int T;
    cin >> T;
    while(T--) solve();
    return 0;
}
/*

*/

可以过(都是 LL),而

#include <bits/stdc++.h>
#define LL long long
using namespace std;
int n, p, q;
inline int read()
{
	int s = 0, w = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9'){ if(ch == '-'){w = -1;} ch = getchar();}
	while(ch >= '0' && ch <= '9'){s = (s << 3) + (s << 1) + (ch ^ 48); ch = getchar();}
	return s * w;
}
inline void write(int x)
{
    if(x < 0)
	{
        putchar('-');
        x = -x;
    }
    if(x > 9)
		write(x / 10);
    putchar(x % 10 + '0');
}
void solve()
{
    n = read(), p = read(), q = read();
    if(p == 0) return cout << 0 << endl, void();
    // if(q == 0) return cout << (n & 1 ? p : 0) << endl, void();
    if(n == 1) return cout << p << endl, void();
    int pos = (q / p - 1) / 2;
    // cout << pos << endl;
    LL num = (n - pos) / 2 * q;
    // cout << num << endl;
    LL ans = (p * (1 + pos) * pos / 2) + ((n - pos) & 1 ? num + (pos + 1) * p : num);
    write(ans);
    printf("\n");
    // printf("%lld\n", ans);
}
signed main()
{
    int T;
    cin >> T;
    while(T--) solve();
    return 0;
}
/*

*/

这个过不了。

差别在于过不了的那份代码 n,p,qn,p,q 没有开 LL。

如果是第二份代码最后的大数据会出负数。

求教 /bx

2024/12/28 21:42
加载中...