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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章