SPOJ AMR10I Dividing Stones --DFS

题意:给n个石头,分成一些部分(最多n部分,随便分),问分完后每部分的数量的乘积有多少种情况。

分析:可以看出,其实每个乘积都可以分解为素数的乘积,比如乘积为4,虽然可以分解为4*1,但是更可以分解为2*2*1,所以就可以枚举素因子来分解,dfs即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#define ll long long
using namespace std;
#define N 100007

int prime[22] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71};
int n,p;
set<ll> ans;

void dfs(int ind,int now,ll num)
{
    ans.insert(num);
    if(now < prime[ind])
        return;
    dfs(ind,now-prime[ind],num*prime[ind]%p);  //分解这个
    dfs(ind+1,now,num);  //不分解这个
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&p);
        ans.clear();
        dfs(0,n,1);
        printf("%d\n",ans.size());
    }
    return 0;
}

SPOJ AMR10I Dividing Stones --DFS

时间: 2024-12-04 17:10:53

SPOJ AMR10I Dividing Stones --DFS的相关文章

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

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 a

HDU 6060 RXD and dividing(dfs 思维)

RXD and dividing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1893    Accepted Submission(s): 809 Problem Description RXD has a tree T, with the size of n. Each edge has a cost.Define f(S) 

CodeForces 264A Escape from Stones dfs

题目链接:点击打开链接 题意:开始有一个区间[0,1] 每次操作在中间填i,然后选择坐半段或者右半段(给出选择的方案,然后从左到右输出填写的i) (i=1 2 3···) #include <cstdio> char s[1000005]; void dfs(int x){ if(s[x] == 0)return ; if(s[x] == 'l') { dfs(x+1); printf("%d\n", x+1); } else { printf("%d\n&quo

SPOJ AMR10I 递归

DES :给你n 块石头.不会超过70.把它们分成n堆.每堆里的石头数做积.问共有多少个数.最终的结果除了1之外都能分解成素数相乘或者素数相乘再乘1.所以可以找到所有不超过70的素数然后进行深搜. 感觉深搜好难好难好难.... #include<stdio.h> #include<iostream> #include<set> using namespace std; int prime[21] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29

1014 Dividing dp/dfs

先用dfs方法:(注意剪枝的重要性) #include<iostream> #include<cstdio> #include<cmath> using namespace std; int sumvalue,halfvalue; int n[7]; bool flag;//记录是否能够进行平分. void dfs(int value,int c){ if(flag) return; if(value==halfvalue){ flag=true; return; }

暑假集训-个人赛第四场

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

UESTC 2014 Summer Training #16 Div.2

虽然被刷了还是要继续战斗下去嗯...就是基础不好,难度相对较大 A.SPOJ AMR10A 点是顺时针给出的,可以在图上画画(脑补也行),连线x-a,x-b(x为选定的一个点,比如第一个点),就把所求面积分成了四部分,a-b左边部分是较容易求出来的, 三角形面积是直接可求,另外两个多边形面积是可以预处理出来的(多个三角形面积和) 反正我是沒想出來...看題解也理解半天,多邊形面積转化为三角形面积和 嗯嗯 #include <iostream> #include <cstdio> #

HDU 6060 17多校3 RXD and dividing(树+dfs)

Problem Description RXD has a tree T, with the size of n. Each edge has a cost.Define f(S) as the the cost of the minimal Steiner Tree of the set S on tree T. he wants to divide 2,3,4,5,6,…n into k parts S1,S2,S3,…Sk,where ?Si={2,3,…,n} and for all d