数学spoj8063

I - Dividing Stones
Time Limit:7000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

There are N stones, which can be divided into some piles arbitrarily. Let the value of each division be equal to the product of the number of stones in all the piles modulo P. How many possible distinct values are possible for a given N and P? 
 
INPUT 
The first line contains the number of test cases T. T lines follow, one corresponding to each test case, containing 2 integers: N and P. 
 
OUTPUT 
Output T lines, each line containing the required answer for the corresponding test case. 
 
CONSTRAINTS
T <= 20 
2 <= N <= 70 
2 <= P <= 1e9 
 
SAMPLE INPUT

3 1000 
5 1000 
 
SAMPLE OUTPUT 


 
EXPLANATION
In the first test case, the possible ways of division are (1,1,1), (1,2), (2,1) and (3) which have values 1, 2, 2, 3 and hence, there are 3 distinct values. 
In the second test case, the numbers 1 to 6 constitute the answer and they can be obtained in the following ways: 
1=1*1*1*1*1 
2=2*1*1*1 
3=3*1*1 
4=4*1 
5=5 
6=2*3



#include<stdio.h>
#include<set>
#include<algorithm>
using namespace std;
int n,p;
set<long long> S;
int pr[]={2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73};
void dfs(int x,int n,long long ji)
{
    S.insert(ji);
    if(pr[x]>n) return;
    dfs(x,n-pr[x],ji*pr[x]%p);//取第x个素数
    dfs(x+1,n,ji);//从第x+1个素数深搜
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        S.clear();
        scanf("%d%d",&n,&p);
        dfs(0,n,1);
        printf("%d\n",S.size());
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值