ZOJ - 3629 Treasure Hunt IV (打表找规律)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3629

题意:问某个区间内这样的数 n 有几个,n 满足: [n/1] +[n/2] + ... + [n/n] 是偶数

思路:打出100的表就可以找出规律,发现0,4-8,16-24,36-48,64-80都是可以的,所以推出n^2~(n+1)^2  (n是偶数)的区间都是可以的。

代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define eps 1e-7
#define fuck(x) cout<<"<"<<x<<">"<<endl
#define fi first
#define se second
#define pb push_back
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const double PI = acos(-1.0);
const LL INFLL = 0x3f3f3f3f3f3f3f3fll;
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;


ull a,b;
ull f(ull n){
    if (n==-1) return 0;
    ull ans=0;
    ull x=sqrt(n);
    if (x&1){
        ans+=((x+1)/2)*x;
    }
    else {
        ans+=(n-x*x+1);
        ans+=(x/2)*(x-1);
    }
    return ans;
}
int main() {
    while (cin>>a>>b){
        cout<<f(b)-f(a-1)<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值