hdu 3304 Interesting Yang Yui Triangle

题意:

给出P,N,问第N行的斐波那契数模P不等于0的有多少个?

限制:

P < 1000,N <= 10^9

思路:

lucas定理,

如果:

n = a[k]*p^k + a[k-1]*p^(k-1) + ... + a[1]*p + a[0]

m = b[k]*p^k + b[k-1]*p^(k-1) + ... + b[1]*p + b[0]

则:

C(n,m) = pe(i=0~k,C(a[i],b[i]))%p 其中pe表示连乘符号。

由于n已经确定,所以a[i] (0 <= i <= k)已经确定,所以我们只需要找出每个a[i]有多少种b[i],使得C(a[i],b[i])%P!=0,暴力一遍就可以了。

/*hdu 3304 Interesting Yang Yui Triangle
  题意:
  给出P,N,问第N行的斐波那契数模P不等于0的有多少个?
  限制:
  P < 1000,N <= 10^9
  思路:
  lucas定理,
  如果:
  n = a[k]*p^k + a[k-1]*p^(k-1) + ... + a[1]*p + a[0]
  m = b[k]*p^k + b[k-1]*p^(k-1) + ... + b[1]*p + b[0]
  则:
  C(n,m) = pe(i=0~k,C(a[i],b[i]))%p 其中pe表示连乘符号。

  由于n已经确定,所以a[i] (0 <= i <= k)已经确定,所以我们只需要找出每个a[i]有多少种b[i],使得C(a[i],b[i])%P!=0,暴力一遍就可以了。
 */
#include<iostream>
#include<cstdio>
using namespace std;
#define LL long long
const int MOD=10000;
const int N=105;
int a[N];
int cnt=0;
int ny[N];
LL inv(LL a,LL m){
	LL p=1,q=0,b=m,c,d;
	while(b>0){
		c=a/b;
		d=a; a=b; b=d%b;
		d=p; p=q; q=d-c*q;
	}
	return p<0?p+m:p;
}

void predo(int p){
	ny[0]=1;
	for(int i=1;i<p;++i){
		ny[i]=inv(i,p);
	}
}
LL deal(int x,int p){
	LL ret=0;
	LL cur=1%p;
	if(cur) ++ret;
	for(int i=1;i<=x;++i){
		cur=cur*ny[i]%p*(x-i+1)%p;
		if(cur) ++ret;
	}
	return ret;
}
void gao(int p, int n){
	cnt=0;
	while(n){
		a[cnt++]=n%p;
		n/=p;
	}
	LL ans=1;
	for(int i=0;i<cnt;++i){
		ans=ans*deal(a[i],p)%MOD;
	}
	printf("%04lld\n",ans);
}
int main(){
	int p, n;
	int cas=0;
	while(scanf("%d%d", &p, &n) && (p||n)){
		predo(p);
		printf("Case %d: ",++cas);
		gao(p, n);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-22 18:03:44

hdu 3304 Interesting Yang Yui Triangle的相关文章

POJ 3146 &amp; HDU 3304 Interesting Yang Yui Triangle(杨辉三角)

题目链接: HDU 3304 :http://acm.hdu.edu.cn/showproblem.php?pid=3304 POJ 3146  :http://poj.org/problem?id=3146 Problem Description Harry is a Junior middle student. He is very interested in the story told by his mathematics teacher about the Yang Hui trian

HDU 3304 Interesting Yang Yui Triangle lucas定理

输入p n 求杨辉三角的第n+1行不能被p整除的数有多少个 Lucas定理: A.B是非负整数,p是质数.AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0]. 则组合数C(A,B)与C(a[n],b[n])*C(a[n-1],b[n-1])*...*C(a[0],b[0])  mod p同余 即:Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p),在存在i.b[i]>a[i]时,mod值为0,所以必然整除.当对于全部i,b

Interesting Yang Yui Triangle(hdu3304)

Interesting Yang Yui Triangle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 332    Accepted Submission(s): 199 Problem Description Harry is a Junior middle student. He is very interested in th

HDU Interesting Yang Yui Triangle (Lucas定理)

题意:求杨辉三角中第 n+1行不能整除 p的数目. 析:运用Lucas定理,只要统计C(ni, mi)中全都不是0的数目即可,因为是第 n+1行,所以ni每次都不变,也就是mi <= ni,那么C(ni, mi),就不是0. 所以就有ni+1种答案,最后乘起来即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string>

LA3700 Interesting Yang Hui Triangle(Lucas定理)

Harry is a Junior middle student. He is very interested in the story told by his mathematics teacher about the Yang Hui triangle in the class yesterday. After class he wrote the following numbers to show the triangle our ancestor studied. 1 1 1 1 2 1

hdu 2814 Interesting Fibonacci

Interesting Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 712    Accepted Submission(s): 137 Problem Description In mathematics, the Fibonacci numbers are a sequence of numbers name

hdu 5785 Interesting(manacher+前缀和)

题目链接:hdu 5785 Interesting 题意: 有一个长度为n的串(n<=10^6),对 1 <= i <= j < k <= length(s) . 如果[i,j]和[j+1,k]都是回文串.则对答案的贡献为 i*k ,求贡献和. 题解: 详细题解传送门 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 type

HDU 2426 Interesting Housing Problem(KM完美匹配)

HDU 2426 Interesting Housing Problem 题目链接 题意:n个学生,m个房间,给定一些学生想住房的喜欢度,找一个最优方案使得每个学生分配一个房间,并且使得喜欢度最大,注意这题有个坑,就是学生不会住喜欢度为负的房间 思路:很明显的KM最大匹配问题,喜欢度为负直接不连边即可 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm>

hdu 2426 Interesting Housing Problem (KM算法)

Interesting Housing Problem Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2388    Accepted Submission(s): 879 Problem Description For any school, it is hard to find a feasible accommodation