刷题总结——跳蚤(poj1091容斥+分解质因数)

题目:

Description

Z城市居住着很多只跳蚤。在Z城市周六生活频道有一个娱乐节目。一只跳蚤将被请上一个高空钢丝的正中央。钢丝很长,可以看作是无限长。节目主持人会给该跳蚤发一张卡片。卡片上写有N+1个自然数。其中最后一个是M,而前N个数都不超过M,卡片上允许有相同的数字。跳蚤每次可以从卡片上任意选择一个自然数S,然后向左,或向右跳S个单位长度。而他最终的任务是跳到距离他左边一个单位长度的地方,并捡起位于那里的礼物。 
比如当N=2,M=18时,持有卡片(10, 15, 18)的跳蚤,就可以完成任务:他可以先向左跳10个单位长度,然后再连向左跳3次,每次15个单位长度,最后再向右连跳3次,每次18个单位长度。而持有卡片(12, 15, 18)的跳蚤,则怎么也不可能跳到距他左边一个单位长度的地方。 
当确定N和M后,显然一共有M^N张不同的卡片。现在的问题是,在这所有的卡片中,有多少张可以完成任务。 

Input

两个整数N和M(N <= 15 , M <= 100000000)。

Output

可以完成任务的卡片数。

Sample Input

2 4

Sample Output

12

Hint

这12张卡片分别是: 
(1, 1, 4), (1, 2, 4), (1, 3, 4), (1, 4, 4), (2, 1, 4), (2, 3, 4), 
(3, 1, 4), (3, 2, 4), (3, 3, 4), (3, 4, 4), (4, 1, 4), (4, 3, 4) 

题解:

首先很明显几个数要满足条件最大质因数要为1·····

然后就是分解质因数m,用容斥搞搞就出来了···

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#define ul unsigned long long
using namespace std;
vector<int>zhiyinzi;
ul ans=0,n,m;
inline ul ksm(ul a,ul b)
{
  ul temp=1;
  while(b)
  {
    if(b%2==1)  temp=temp*a;
    b=b/2;
    a=a*a;
  }
  return temp;
}
inline void dfs(int u,int tot,int f)
{
  if(u==zhiyinzi.size())    
  {
    ul temp=m/tot;
    ans+=f*ksm(temp,n);
    return;
  }
  dfs(u+1,tot*zhiyinzi[u],-f);
  dfs(u+1,tot,f);
}
int main()
{
  //freopen("a.in","r",stdin);
  cin>>n>>m;  
  ul temp=m;
  for(int i=2;i*i<=temp;i++)
  {
    if(temp%i==0)
    {
      zhiyinzi.push_back(i);  
      while(temp%i==0)  temp/=i;
    }
  }
  if(temp!=1)  zhiyinzi.push_back(temp);
  dfs(0,1,1);
  cout<<ans<<endl;
  return 0; 
}

 

posted @ 2017-10-07 15:09  AseanA  阅读(358)  评论(0编辑  收藏  举报