http://acm.hdu.edu.cn/showproblem.php?pid=4135
题意:求[a,b]范围内与n互质的数的个数。
思路:这题数据貌似很弱,用欧拉函数算下就知道10^9的质因数的个数最大约等于10^9,这里开大了显然爆内存,1005居然也能过。。
和hdu1286类似,不同点在于那题是1~n-1内与集合不互质的数,而这里是[a,b]内,而且a和b的范围相当大。此外,欧拉函数只能求1~n-1内的质数数量,对区间内的质数数量没有任何办法,注意改变的是区间而不是欧拉值,定义要看好(这里栽了我好久)。欧拉函数不能用,那只能用容斥了。[a,b] = [1,b]-[1,a-1],那么同时算出1~b内与n不互质的数的个数和1~a-1内与n不互质的数的个数,两者相减即可。
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 1005;
ll a, b, n, cnt, num[N], ansa, ansb;
void euler(ll n)
{
for(int i = 2; i*i <= n; i++)
{
if(n%i == 0)
{
num[cnt++] = i;
while(n%i == 0)
n /= i;//彻底消除当前素因子
}
}
if(n > 1) num[cnt++] = n;
}
ll gcd(ll a, ll b)
{
if(b == 0) return a;
return gcd(b, a%b);
}
ll lcm(ll a, ll b)
{
return a*b/gcd(a, b);
}
void dfs(ll th, ll now, ll step)
{
if(step > cnt) return;
ll LCM = lcm(now, num[th]);
if(step&1)
{
ansa += a/LCM;
ansb += b/LCM;
}
else
{
ansa -= a/LCM;
ansb -= b/LCM;
}
for(ll i = th+1; i < cnt; i++)
dfs(i, LCM, step+1);
}
int main()
{
// freopen("in.txt", "r", stdin);
int t, Case = 1;
scanf("%d", &t);
while(t--)
{
cnt = 0;
ansa = ansb = 0;
scanf("%lld%lld%lld", &a, &b, &n);
a-=1;
euler(n);
for(int i = 0; i < cnt; i++)
{
dfs(i, num[i], 1);
}
printf("Case #%d: %lld\n", Case++, (b-a) - (ansb-ansa));
}
return 0;
}
此外还学习了一种队列数组的写法,大致思想和搜索一致,就是排列组合的种类全部存到了队列里。代码有注释。
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 1005;
ll cnt, num[N];
void euler(ll n)//求出所有质因子
{
for(int i = 2; i*i <= n; i++)
{
if(n%i == 0)
{
num[cnt++] = i;//保存所有质因子
while(n%i == 0)
n /= i;//彻底消除当前素因子
}
}
if(n > 1) num[cnt++] = n;
}
ll exclusion(ll m)//容斥,求出1~m内与n不互质的数的数目
{
ll que[110], ans;//保存所有质因子的排列组合
int top = 0, t;
que[top++] = -1;
ans = 0;
for(int i = 0; i < cnt; i++)//遍历所有质因子
{
t = top;
for(int j = 0; j < t; j++)
{
que[top++] = que[j]*(-1)*num[i];
}
}
for(int i = 1; i < top; i++)
ans+=(m/que[i]);
return ans;
}
int main()
{
// freopen("in.txt", "r", stdin);
int t, Case = 1;
ll a, b, n;
scanf("%d", &t);
while(t--)
{
cnt = 0;
scanf("%lld%lld%lld", &a, &b, &n);
a-=1;
euler(n);
printf("Case #%d: %lld\n", Case++, (b-a) - (exclusion(b)-exclusion(a)));
}
return 0;
}