SPOJ AMR10I Dividing Stones
Time limit: 7s Source limit: 50000B Memory limit: 256MB
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
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个石子,可以分成任意堆,每一种分法的值为每一堆的石子数量的乘积。求一共可以分成多少个不同的乘积。
分析:最终的乘积除了1以外,都可以分解成素数相乘或者素数相乘再与1相乘的形式。因为n不超过70,所以我们可以先找出不超过70的所有素数,然后从这些素数中进行搜索求解即可。为了方便求出不同的乘积有多少个,可以用STL里面的set来统计不同的数有多少个。
#include<cstdio> #include<cstring> #include<set> #include<algorithm> using namespace std; typedef long long LL; set<LL> s; int prime[21] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73}; int n, p; void dfs(int num, int cur, LL ans) { s.insert(ans); if(cur < prime[num]) return ; dfs(num, cur - prime[num], ans * prime[num] % p); //要第num个素数 dfs(num+1, cur, ans); //不要第num个素数 } int main() { int T, i, j; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&p); s.clear(); dfs(0, n, 1); printf("%d\n", s.size()); } printf("\n"); }
分类:
数论,计算几何
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!