2017.10.27 T3 2015
樣例數據1
輸入
2 3
輸出
8
樣例數據2
輸入
8 8
輸出
16711680
分析:T3很難,很難,難到你讀不懂題,也就只難在讀不懂題上。如果能讀懂題目,這道題就是個容斥原理裸題,但是這道題TMD題面就是一篇記敘文,還是哲理類的!!!整道題的題面就佔了一頁,完全看不下去啊,而且在前面的題花的時間太多了,也沒法慢慢理解這道題,就放棄了。
代碼
不用看了,就是跳蚤加一個取模,優化了一下篩質數
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<iomanip>
#include<queue>
#include<set>
using namespace std;
long long getlong()
{
long long sum=0,f=1;
char ch;
for(ch=getchar();(ch<'0'||ch>'9')&&ch!='-';ch=getchar());
if(ch=='-')
{
f=-1;
ch=getchar();
}
for(;ch>='0'&&ch<='9';ch=getchar())
sum=(sum<<3)+(sum<<1)+ch-48;
return sum*f;
}
const int mo=1e9+7;
long long n,m,tot,sushu[55];
long long ans,tmp,a[55];
long long ksm(long long x,long long y)
{
long long res=1;x=x%mo;
for(;y;y=y>>1,x=x*x%mo)
if(y&1)
res=res*x%mo;
return res;
}
void dfs(int cnt,int step,int num)
{
if(step>num)
{
long long res=m;
for(int i=1;i<=num;++i)
res/=a[i];
tmp=(tmp+ksm(res,n))%mo;
return;
}
for(int i=cnt;i<=tot-num+step;++i)
{
a[step]=sushu[i];
dfs(i+1,step+1,num);
}
}
int main()
{
freopen("heal.in","r",stdin);
freopen("heal.out","w",stdout);
n=getlong(),m=getlong();
ans=ksm(m,n);
long long mm=m;
for(long long i=2;i*i<=mm;++i)//把O(n)的篩法改成了根號的篩法
if(mm%i==0)
{
sushu[++tot]=i;
while(mm%i==0)
mm/=i;
}
if(mm>1)
sushu[++tot]=mm;
for(int i=1;i<=tot;++i)
{
tmp=0;
dfs(1,1,i);
if(i&1)
ans=(ans+mo-tmp)%mo;
else
ans=(ans+tmp)%mo;
}
cout<<ans<<'\n';
return 0;
}
本題結。