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 ...
随机推荐
- invalid END header解决方法
我在Windows上的eclipse开发了一个java web项目,然后压缩成war包,通过ftp发送到Linux服务器上,Tomcat先shutdown,再startup.按理说,会在webapps ...
- Windows MySQL5.7安装和配置
http://www.leixuesong.cn/category/mysql MySQL5.7是MySQL是最新的MySQL大版本,也是官方认为目前性能最好的.MySQL5.7也有很多改动,很多新的 ...
- 如何获取堆的dump 的信息,如何分析
获取方式: 1. jdk 自带启动参数 -XX:+HeapDumpBeforeFullGC -XX:HeapDumpPath=/x/x 产生dump日志,然后用visualVm分析 2. jmap 命 ...
- IOS初级:UIView和UIButton
AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDict ...
- 解决textarea 输出有空格问题
我们在使用textarea标签输出的时候,经常会出现前后都有空格.使用trim()处理也不行. 这个原因是因为 我们在编写textarea标签对的时候使用了换行. 解决方法:就是<textare ...
- IPutils
package com.mmall.util; import lombok.extern.slf4j.Slf4j;import org.apache.commons.lang3.StringUtils ...
- mybatis学习 十 动态 SQL
1. 根据方法传入的参数不同执行不同的 SQL 命令.称为动态 SQL, MyBatis 中动态 SQL 就是在 mapper.xml 中添加逻辑判断等. 2. <if>标签 <s ...
- KM匹配板子
/* gyt Live up to every day */ #include<cstdio> #include<cmath> #include<iostream> ...
- 清幽傲竹实现的kbmMWServer数据库联接失败重联(转载红鱼儿)
1.修改kbmMWUnidac单元的TkbmMWUNIDACConnection.InternalOpenConnection方法,加上: //支持unidac重联 ...
- 31、iOS 正则表达式判断UITextField是否为全汉字,全字母,全数字,数字和字母
判断全汉字 if ([self deptNameInputShouldChinese]) { [DemonAlertHelper showToastWithMessage:@"只能是中文&q ...