码迷,mamicode.com
首页 > 其他好文 > 详细

I - Dividing Stones

时间:2014-08-20 13:55:32      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   io   strong   for   

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
2
3 1000
5 1000
 
SAMPLE OUTPUT
3
6
 
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

 

题意:n  p    在1~~n内 寻找素数和等于n的  并且乘积不相等

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<set>
#include <iostream>

using namespace std;
set<long>s;
int p[80],mod,n,t;
bool vis[80];

void prime()
{
    int cnt=0;
    memset(vis,1,sizeof(vis));
    memset(p,0,sizeof(p));
    for(int i=2; i<=80; i++)  ///素数打标   n很小 <=70  所以可以打表
    {
        if(vis[i])
        {
            p[++cnt]=i;
            for(int j=i*i; j<=80; j+=i)
                vis[j]=0;
               // cout<<p[cnt-1]<<"~~~~~~~~~~~"<<endl;
        }

    }
}
void dfs(int i,int x,long long ans)
{
    s.insert(ans);
    if (p[i]>x)
        return;
    dfs(i,x-p[i],ans*p[i]%mod);  ///取第i个素数
    dfs(i+1,x,ans);  ///深搜第i+1个素数
}
int main()
{
    prime();
    scanf("%d",&t);
    while (t--)
    {
        s.clear();
        scanf("%d%d",&n,&mod);
        dfs(1,n,1);
        printf("%d\n",s.size());
    }
    return 0;
}
//dfs(x,n-pr[x],ji*pr[x]%p);//取第x个素数
//dfs(x+1,n,ji);//从第x+1个素数深搜

 

I - Dividing Stones,布布扣,bubuko.com

I - Dividing Stones

标签:des   style   blog   color   os   io   strong   for   

原文地址:http://www.cnblogs.com/zhangying/p/3924294.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!