nefu119 组合素数 算术基本定理的应用

来源:互联网 发布:淘宝官方尺码表 编辑:程序博客网 时间:2024/06/06 03:02

好难啊 看着好难的样子,一开始想直接化简C(2*n,n)的,可是后来发现还是不可以,网上也是没有这道题目的题解的,后来看看题目的名字 叫做组合素数,就去翻了一些相关资料的书,最后居然找到了一个公式,我顿时蛋疼的要死啊,这个公式 我也证不出来,唉~我太水太菜了,不过还是要牢牢记住这个公式,毕竟比赛的时候不可能让你带本书的,据说规定修改了 不能带超过 多少页的 资料,而且必须是只能单面有资料

因为题目中给定的p是素数,所以可以用这个公式,感觉这个式子跟算术基本定理的性质5相关

公式:([2*n/p]-2*[n/p])+([2*n/p^2]-2*[n/p^2])+([2*n/p^3]-2*[n/p^3])+......+([2*n/p^t]-2*[n/p^t])

其中 t=log(p)(2*n),这个log的式子很不好表达这个式子 可以写成 log10(2*n)/log(p),如果觉得我原式子写的不清楚的,可以利用 现在这个式子反化简过去


#include<iostream>#include<cstdio>#include<list>#include<algorithm>#include<cstring>#include<string>#include<queue>#include<stack>#include<map>#include<vector>#include<cmath>#include<memory.h>#include<set>#define ll long long#define LL __int64#define eps 1e-8#define e 2.718281828//const ll INF=9999999999999;#define M 400000100#define inf 0xfffffffusing namespace std;//vector<pair<int,int> > G;//typedef pair<int,int> P;//vector<pair<int,int>> ::iterator iter;////map<ll,int>mp;//map<ll,int>::iterator p;////vector<int>G[30012];int main(void){int t;int n,p;cin>>t;while(t--){cin>>n>>p;double t=log10(2.0*n)/log10(p*1.0);int s=int(t);int ans=0;int cnt=1;int temp=1;while(cnt<=s){temp=temp*p;ans+=(2*n/temp)-2*(n/temp);cnt++;}cout<<ans<<endl;}}