I - Dividing Stones

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

时间: 2024-10-28 13:35:42

I - Dividing Stones的相关文章

SPOJ AMR10I Dividing Stones --DFS

题意:给n个石头,分成一些部分(最多n部分,随便分),问分完后每部分的数量的乘积有多少种情况. 分析:可以看出,其实每个乘积都可以分解为素数的乘积,比如乘积为4,虽然可以分解为4*1,但是更可以分解为2*2*1,所以就可以枚举素因子来分解,dfs即可. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm>

SPOJ AMR 10I Dividing Stones(搜索)

Dividing Stones Time limit: 7s Source limit: 50000B Memory limit: 256MB 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. Ho

暑假集训-个人赛第四场

ID Origin Title   10 / 52 Problem A SPOJ AMR10A Playground     Problem B SPOJ AMR10B Regex Edit Distance     Problem C SPOJ AMR11C Robbing Gringotts   1 / 14 Problem D SPOJ AMR10D Soccer Teams   0 / 3 Problem E SPOJ AMR10E Stocks Prediction   17 / 19

UVa 12525 Boxes and Stones (dp 博弈)

Boxes and Stones Paul and Carole like to play a game with S stones and B boxes numbered from 1 to B. Beforebeginning the game they arbitrarily distribute the S stones among the boxes from 1 to B - 1, leavingbox B empty. The game then proceeds by roun

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

AC日记——Dividing poj 1014

Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 69575   Accepted: 18138 Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbl

uva562 - Dividing coins(01背包)

题目:uva562 - Dividing coins(01背包) 题目大意:给出N个硬币,每个硬币有对应的面值.要求将这些硬币分成两部分,求这两部分最小的差值. 解题思路:先求这些硬币能够凑出的钱(0, 1背包),然后再从sum(这些硬币的总和)/2开始往下找这个值能否由这些硬币凑出.要注意的是,可以由前n个硬币组成那样也是可以组成的面值. 代码: #include <cstdio> #include <cstring> const int N = 105; const int m

hdu 5973 Game of Taking Stones

Game of Taking Stones Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 456    Accepted Submission(s): 174 Problem Description Two people face two piles of stones and make a game. They take turns

poj 1004 Dividing

题目大意是,从输入六个数 ,第i个数代表价值为i的有几个,平均分给两个人 ,明摆着的背包问题,本来以为把他转化为01背包,但是TLe,后来发现是12万的平方还多,所以妥妥的TLE,后来发现这是一个完全背包问题,然后即纠结了 ,没学过啊 ,最后发现思想好i是蛮简单的,水水的A掉了,最后注意一下初始化问题和输入问题后就好了 #include <stdio.h> #include <string.h> int a[10]; int dp[120005]; int maxx(int a,i