【组合数】【Lucas】Number Theory Special Training T3 comb 题解

来源:互联网 发布:积分数据库设计 编辑:程序博客网 时间:2024/06/07 02:03

Problem 3. comb
Input file: comb.in
Output file: comb.out
Time limit: 1 second
Memory limit: 256 MB
一天,Mr. Hu 对组合数产生了兴趣,他想要知道满足下面条件的数i 有多少个:
gcd(
(
n
i
)
; p) = p (0  i  n)
其中p 是素数。
Input
第1 行,2 个整数:n p。
Output
输出所求。
Sample
comb.in comb.out
5 2 1
Note
样例中,一共有5 个组合数:1; 5; 10; 5; 1,其中和2 公约数为2 的数只有一个10,故输出1。
• 对于30% 的数据,满足1  n; p  103
• 对于100% 的数据,满足1  n; p  1018
Page

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<set>#include<queue>#include<algorithm>#include<vector>#include<cstdlib>#include<cmath>#include<ctime>#include<stack>#define INF 2100000000#define LL long long#define clr(x) memset(x,0,sizeof(x))#define ms(a,x) memset(x,a,sizeof(x))#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;int main() {    freopen("comb.in","r",stdin);    freopen("comb.out","w",stdout);    LL n,p,ans1,ans2;    scanf(AUTO""AUTO,&n,&p);    ans1 = n+1; ans2 = 1;    while(n) ans2 *= n%p+1, n /= p;    printf(AUTO"\n",ans1-ans2-1);    return 0;}
0 0