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 }

 

posted @ 2014-09-05 01:01  Naturain  阅读(127)  评论(0编辑  收藏  举报