题目大意:问第n+1层的杨辉三角所有数中有几个数是不能被p整除的;
题目解析:根据lucas定理,要是(n,m)不能被p整除,则n和m转化成p进制后牵着每一位都必须大于等于后者;如果其中有一个不满足,那么就被p整除,所有我们只需要枚举n的每一位,设为a,ans*=ans+1;
AC代码:
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define inf 0x3f3f3f3f
#define ll long long
#define mod 10000
using namespace std;
int main()
{
int p,n;
int kcase=1;
while(scanf("%d%d",&p,&n)!=EOF)
{
if(p==0&&n==0)
return 0;
int ans=1;
while(n)
{
ans=(ans*(n%p+1))%mod;
n=n/p;
}
printf("Case %d: %04d\n",kcase++,ans);
}
return 0;
}
附上错误的TLE但有详细lucas过程的代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
typedef long long LL;
using namespace std;
LL exp_mod(LL a, LL b, LL p) {
LL res = 1;
while(b != 0) {
if(b&1) res = (res * a) % p;
a = (a*a) % p;
b >>= 1;
}
return res;
}
LL Comb(LL a, LL b, LL p) {
if(a < b) return 0;
if(a == b) return 1;
if(b > a - b) b = a - b;
LL ans = 1, ca = 1, cb = 1;
for(LL i = 0; i < b; ++i) {
ca = (ca * (a - i))%p;
cb = (cb * (b - i))%p;
}
ans = (ca*exp_mod(cb, p - 2, p)) % p;
return ans;
}
LL Lucas(int n, int m, int p) {
LL ans = 1;
while(n&&m&&ans) {
if(ans==0) return 0;
ans = (ans*Comb(n%p, m%p, p)) % p;
n /= p;
m /= p;
}
return ans;
}
int main() {
int n, p,cas=1;
while(scanf("%d%d", &p,&n)!=EOF&&n!=0&&p!=0) {
int ans=0;
for(int i=0;i<n/2;i++)
if(Lucas(n,i,p)==0) ans++;
ans*=2;
if(n%2==0)
{
if(Lucas(n,n/2,p)==0) ans++;
}
printf("Case %d: ",cas++);
printf("%04d\n",(n+1-ans)%10000);
}
return 0;
}