您的位置:首页 > 其它

hdu 3208 简单容斥原理

2015-10-12 00:23 330 查看
好题。

学会了一种处理溢出的技巧。。from acdreamer's blog

code:

   
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
using namespace std;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,n,a) for (int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
ll powmod(ll a,ll b) {ll res=1;for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}return res;}
#define Fast_IO ios_base::sync_with_stdio(0);cin.tie(0)
ll n;
const ll T = (ll)1LL<<31;
const ll INF = 1e18 + 300;
ll num[100];
ll multi(ll a, ll b){
ll res = 1;
while(b){
if(b&1){
double tmp = 1.0*INF/res;
if(a > tmp) return -1;
res *= a;
}
b>>=1;
if(b > 0 && a > T) return -1;
a *= a;
}
return res;
}
ll find(ll x, ll k){
ll tmp = pow(x, 1.0/k);
ll p = multi(tmp, k);
if(p == x) return tmp;
if(p == -1 || p > x) return tmp-1;
else{
ll t = multi(tmp+1, k);
if(t != -1&&t<=x) tmp++;
}
return tmp;
}
ll solve(ll x){
memset(num, 0, sizeof(num));
num[1] = x;
for(int i = 2; i<=60; i++)
num[i] = find(x, i)-1;
for(int i=60; i>=1; i--)
for(int j=i+1; j<=60; j++)
if(j%i==0) num[i] -= num[j];
ll res = 0;
for(int i=1; i<=60; i++)
res += num[i]*i;
return res;
}
ll a,b;
int main(){
while(cin>>a>>b&&a+b){
ll res = solve(b) - solve(a-1);
printf("%lld\n", res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  小学生容斥