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[],mod,n,t;
bool vis[]; void prime()
{
int cnt=;
memset(vis,,sizeof(vis));
memset(p,,sizeof(p));
for(int i=; i<=; i++) ///素数打标 n很小 <=70 所以可以打表
{
if(vis[i])
{
p[++cnt]=i;
for(int j=i*i; j<=; j+=i)
vis[j]=;
// 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+,x,ans); ///深搜第i+1个素数
}
int main()
{
prime();
scanf("%d",&t);
while (t--)
{
s.clear();
scanf("%d%d",&n,&mod);
dfs(,n,);
printf("%d\n",s.size());
}
return ;
}
//dfs(x,n-pr[x],ji*pr[x]%p);//取第x个素数
//dfs(x+1,n,ji);//从第x+1个素数深搜
I - Dividing Stones的更多相关文章
- SPOJ AMR10I Dividing Stones --DFS
题意:给n个石头,分成一些部分(最多n部分,随便分),问分完后每部分的数量的乘积有多少种情况. 分析:可以看出,其实每个乘积都可以分解为素数的乘积,比如乘积为4,虽然可以分解为4*1,但是更可以分解为 ...
- SPOJ AMR10I Dividing Stones
Time limit: 7s Source limit: 50000B Memory limit: 256MB The first line contains the number of test c ...
- 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. ...
- POJ 1014 Dividing
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 66032 Accepted: 17182 Descriptio ...
- CF 371B Fox Dividing Cheese[数论]
B. Fox Dividing Cheese time limit per test 1 second memory limit per test 256 megabytes input standa ...
- AC日记——Dividing poj 1014
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 69575 Accepted: 18138 Descri ...
- POJ 1014 Dividing(多重背包)
Dividing Description Marsha and Bill own a collection of marbles. They want to split the collectio ...
- Dividing a Chocolate(zoj 2705)
Dividing a Chocolate zoj 2705 递推,找规律的题目: 具体思路见:http://blog.csdn.net/u010770930/article/details/97693 ...
- 动态规划--模板--hdu 1059 Dividing
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- BZOJ1833或洛谷2602 [ZJOI2010]数字计数
BZOJ原题链接 洛谷原题链接 又是套记搜模板的时候.. 对\(0\sim 9\)单独统计. 定义\(f[pos][sum]\),即枚举到第\(pos\)位,前面枚举的所有位上是当前要统计的数的个数之 ...
- Vsphere初试——使用Vsphere client
好不容易安装好ESXi之后,就要安装一个Vsphere Client,为什么要安装这个东东.使用过vmware workstation的人都知道,安装完就可以添加虚拟机,但是ESXi要通过Vspher ...
- [都是原创]Php simplexml 添加节点
Php simplexml 添加节点 原始代码如下 //================<? //创建xml对象$xml = new SimpleXMLElement('<Messages ...
- Python 递归函数 详解
Python 递归函数 详解 在函数内调用当前函数本身的函数就是递归函数 下面是一个递归函数的实例: 第一次接触递归函数的人,都会被它调用本身而搞得晕头转向,而且看上面的函数调用,得到的结果会 ...
- MYSQL索引类型+索引方法
MYSQL索引有四种 PRIMARY(唯一且不能为空:一张表只能有一个主键索引). INDEX(普通索引). UNIQUE(唯一性索引). FULLTEXT(全文索引:用于搜索很长一篇文章的时候,效果 ...
- mysql查看某个表的列名
mysql查看某个表的列名mysql -uusername -p 输入密码按登录mysqlshow databases; 查看有哪些数据库use dbname; 选择数据库show tables:查看 ...
- 添加/删除-HTML DOM 常用对象 -BOM-打开和关闭窗口- history-location
1. 添加/删除 3步: 1. 添加一个空元素 var a=document.createElement("a"); <a></a> 2. 定义元素的关键属 ...
- 20 模块之 re subprocess
re: 什么是正则: 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则.(在Python中)它内嵌在Python中,并通过 ...
- unity中的构造函数
避免使用构造函数 不要在构造函数中初始化任何变量,使用Awake或Start实现这个目的.即使是在编辑模式中Unity也自动调用构造函数,这通常发生在一个脚本被编译之后,因为需要调用构造函数来取向一个 ...
- sql number类型和varchar2类型
查询时,发现org_id 为number类型,zone_id为varchar2类型,需要转化 转换 to_char(),或者to_number select a.id,b.col,a.col from ...