没写返回值,但是算出正确答案?
  • 板块学术版
  • 楼主__vector__
  • 当前回复5
  • 已保存回复5
  • 发布时间2025/7/24 08:17
  • 上次更新2025/7/24 13:37:11
查看原帖
没写返回值,但是算出正确答案?
507348
__vector__楼主2025/7/24 08:17

我刚刚完成了对上次 Edu 的 vp。

其中 C 题,我赛时有一发提交本题过了样例,但是提交 TLE。

题面传送门

代码如下:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(auto i=(a);i<=(b);i++)
#define REP(i,a,b) for(auto i=(a);i>=(b);i--)
#define FORK(i,a,b,k) for(auto i=(a);i<=(b);i+=(k))
#define REPK(i,a,b,k) for(auto i=(a);i>=(b);i-=(k))
#define pb push_back
#define mkpr make_pair
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
template<class T>
void ckmx(T& a,T b){
    a=max(a,b);
}
template<class T>
void ckmn(T& a,T b){
    a=min(a,b);
}
template<class T>
T gcd(T a,T b){
    return !b?a:gcd(b,a%b);
}
template<class T>
T lcm(T a,T b){
    return a/gcd(a,b)*b;
}
#define gc getchar()
#define eb emplace_back
#define pc putchar
#define ep empty()
#define fi first
#define se second
#define pln pc('\n');
#define islower(ch) (ch>='a'&&ch<='z')
#define isupper(ch) (ch>='A'&&ch<='Z')
#define isalpha(ch) (islower(ch)||isupper(ch))
template<class T>
void wrint(T x){
    if(x<0){
        x=-x;
        pc('-');
    }
    if(x>=10){
        wrint(x/10);
    }
    pc(x%10^48);
}
template<class T>
void wrintln(T x){
    wrint(x);
    pln
}
template<class T>
void read(T& x){
    x=0;
    int f=1;
    char ch=gc;
    while(!isdigit(ch)){
        if(ch=='-')f=-1;
        ch=gc;
    }
    while(isdigit(ch)){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=gc;
    }
    x*=f;
}
void ioopti(){
    ios::sync_with_stdio(0);
    cin.tie(0);
}
const ll p[4]={2,3,5,7};
ll f(ll r){
    ll ans=0;
    int all=(1<<4)-1;
    FOR(msk,1,all){
        ll mul=1;
        FOR(i,0,3){
            if(msk&(1<<i)){
                mul*=p[i];
            }
        }
        ll flg;
        if(__builtin_parity(msk))flg=1;
        else flg=-1;
        ans+=flg*(r/mul);
    }
    ans=r-ans;
}
void solve(int id_of_test){
	ll l,r;
    read(l);
    read(r);
    printf("%lld\n",f(r)-f(l-1));
}
int main()
{
	int T;
	read(T);
	FOR(_,1,T){
		solve(_);
	}
	return 0;
}
/*
1. 对题意的理解能否和样例对的上?  
2. 每一步操作,能否和自己的想法对应上?  
3. 每一步操作的正确性是否有保证?  
4. 是否考虑到了所有的 case?特别是极限数据。
5. 变量的数据类型是否与其值域匹配?
6. 时间复杂度有保证吗?
7. 空间多少 MB?  
*/

设计思想是差分 + 容斥。

可以注意到, f 函数并没有返回 ans,但是程序在本地(Windows 64bit,g++ -std=c++20)却能正确算出正确答案。

求助 dalao 们,有人知道这是怎么回事吗?

2025/7/24 08:17
加载中...