Hdu--1695(数论,欧拉函数,容斥原理)

2014-09-05 00:59:31

看了别人的博客,感觉此容斥写的相当精巧,给赞!相关题解

 1 /*************************************************************************
 2     > File Name: hdu1695.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com
 5     > Created Time: Thu 04 Sep 2014 08:12:03 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 typedef long long ll;
16 const int RA = 100000;
17 
18 int a,b,c,d,k;
19 int Case;
20 int pfac[RA + 5][30];
21 int num[RA + 5];
22 ll phi[RA + 5];
23 ll sum[RA + 5];
24 
25 void Phi_table(){
26     phi[1] = sum[1] = 1;
27     for(ll i = 2; i <= RA; ++i){
28         if(!phi[i]){
29             for(ll j = i; j <= RA; j += i){
30                 if(!phi[j]) phi[j] = j;
31                 phi[j] = phi[j] / i * (i - 1);
32                 pfac[j][num[j]++] = i;
33             }
34         }
35         sum[i] = sum[i - 1] + phi[i];
36     }
37 }
38 
39 ll Dfs(int pos,int val,int st){
40     ll res = 0;
41     for(int i = pos; i < num[val]; ++i)
42         res += st / pfac[val][i] - Dfs(i + 1,val,st / pfac[val][i]);
43     return res;
44 }
45 
46 int main(){
47     Phi_table();
48     scanf("%d",&Case);
49     for(int t = 1; t <= Case; ++t){
50         scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
51         if(k == 0){
52             printf("Case %d: 0\n",t);
53             continue;
54         }
55         if(b > d){
56             int tem = b;
57             b = d;
58             d = tem;
59         }
60         b /= k;
61         d /= k;
62         ll ans = sum[b];
63         for(int i = b + 1; i <= d; ++i)
64             ans += b - Dfs(0,i,b);
65         printf("Case %d: %I64d\n",t,ans);
66     }
67     return 0;
68 }

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3957131.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值