[HNOI 2002]跳蚤

Description

Z城市居住着很多只跳蚤。在Z城市周六生活频道有一个娱乐节目。一只跳蚤将被请上一个高空钢丝的正中央。钢丝很长,可以看作是无限长。节目主持人会给该跳蚤发一张卡片。卡片上写有N+1个自然数。其中最后一个是M,而前N个数都不超过M,卡片上允许有相同的数字。跳蚤每次可以从卡片上任意选择一个自然数S,然后向左,或向右跳S个单位长度。而他最终的任务是跳到距离他左边一个单位长度的地方,并捡起位于那里的礼物。比如当N=2,M=18时,持有卡片(10, 15, 18)的跳蚤,就可以完成任务:他可以先向左跳10个单位长度,然后再连向左跳3次,每次15个单位长度,最后再向右连跳3次,每次18个单位长度。而持有卡片(12, 15, 18)的跳蚤,则怎么也不可能跳到距他左边一个单位长度的地方。当确定N和M后,显然一共有MN张不同的卡片。现在的问题是,在这所有的卡片中,有多少张可以完成任务。

Input

输入文件有且仅有一行,包括用空格分开的两个整数N和M。

Output

输出文件有且仅有一行,即可以完成任务的卡片数。 

Sample Input

2 4

Sample Output

12

题解

该死的bz(婊子)oj没用官方数据,还要用高精度。

一张可行的卡片,就是将上面的数字经过加减变换能够得到$1$,于是我们可以猜想一下卡片上的数字所需满足的要求。首先很容易猜到,这些数字必然有奇数有偶数,但是仅满足这个条件是显然不够的,如$3$,$6$就无法得出$1$来,于是可以继续猜想这些数字两两互质,即所有数字的公因数为$1$,用数论的方法可以证明这个猜想是正确的。然后可以用容斥原理来算出答案,这里举个例子来说明,假设$m=30=2*3*5$,答案=$m^n$-(有公因数$2$的$n$元组)-(有公因数$3$的$n$元组)-(有公因数$5$的$n$元组)+(有公因数$2$,$3$的$n$元组)+(有公因数$2$,$5$的$n$元组)+(有公因数$3$,$5$的$n$元组)-(有公因数$2$,$3$,$5$的$n$元组)

 1 #include <set>
 2 #include <map>
 3 #include <ctime>
 4 #include <cmath>
 5 #include <queue>
 6 #include <stack>
 7 #include <vector>
 8 #include <cstdio>
 9 #include <string>
10 #include <cstring>
11 #include <cstdlib>
12 #include <iostream>
13 #include <algorithm>
14 #define LL long long
15 #define Max(a, b) ((a) > (b) ? (a) : (b))
16 #define Min(a, b) ((a) < (b) ? (a) : (b))
17 using namespace std;
18 const int INF = ~0u>>1;
19 
20 LL n, m;
21 LL ans = 0;
22 LL q[1005], tot;
23 
24 LL pow(LL a, LL b){
25     LL c = 1;
26     while (b){
27       if (b&1) c *= a;
28       b >>= 1;
29       a *= a;
30     }
31     return c;
32 }
33 void sperate(LL m){
34     for (LL i = 2; i*i <= m; i++)
35     if (m%i == 0){
36         q[++tot] = i;
37         while (m%i == 0) m /= i;
38     }
39     if (m-1) q[++tot]=m;
40 }
41 void dfs(int cen, LL cnt, int cho){
42     if (cen > tot){
43       if (cho == 0) return;
44       if (cho%2) ans -= pow(m/cnt, n);
45       else ans += pow(m/cnt, n);
46       return;
47     }
48     dfs(cen+1, cnt, cho);
49     dfs(cen+1, cnt*q[cen], cho+1);
50 }
51 
52 int main(){
53     scanf("%lld%lld", &n, &m);
54     ans = pow(m, n);
55     sperate(m);
56     dfs(1, 1, 0);
57     printf("%lld
", ans);
58     return 0;
59 }
原文地址:https://www.cnblogs.com/NaVi-Awson/p/7469439.html