FZUOJ Problem 1607 Greedy division (求因子个数)
2016-01-10 16:18
267 查看
Problem 1607 Greedy division
Accept: 478 Submit: 1715
Oaiei has inherited a large sum of wealth recently; this treasure has n pieces of golden coins. Unfortunately, oaiei can not own this wealth alone, and he must divide this wealth into m parts (m>1). He can only get one of the
m parts and the m parts have equal coins. Oaiei is not happy for only getting one part of the wealth. He would like to know there are how many ways he can divide the wealth into m parts and he wants as many golden coins as possible. This is your question,
can you help him?
There are multiply tests, and that will be 500000. For each test, the first line is a positive integer N(2 <= N <= 1000000), N indicates the total number of golden coins in the wealth.
For each test, you should output two integer X and Y, X is the number of ways he can divide the wealth into m parts where m is large than one, Y is the number of golden coins that he can get from the wealth.
568
1 13 33 4
There are huge tests, so you should refine your algorithm.
题意:有n个钱,然后要平分,即每一份的数量都相同,问有多少种分法,每份最多多少钱
思路:转换为求n的因子个数和最小的因子问题,用快速求一个数因子个数的方法
一个数n = p1^a1*p2^a2...pk^ak,那么他的因子个数为(a1+1)*(a2+1)*(a3+1)*...*(ak+1),为了快速,先打一个素数表
总结:写了一发超时,然后发现了这个方法。。记住了
ac代码:
Accept: 478 Submit: 1715
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
Oaiei has inherited a large sum of wealth recently; this treasure has n pieces of golden coins. Unfortunately, oaiei can not own this wealth alone, and he must divide this wealth into m parts (m>1). He can only get one of them parts and the m parts have equal coins. Oaiei is not happy for only getting one part of the wealth. He would like to know there are how many ways he can divide the wealth into m parts and he wants as many golden coins as possible. This is your question,
can you help him?
Input
There are multiply tests, and that will be 500000. For each test, the first line is a positive integer N(2 <= N <= 1000000), N indicates the total number of golden coins in the wealth.
Output
For each test, you should output two integer X and Y, X is the number of ways he can divide the wealth into m parts where m is large than one, Y is the number of golden coins that he can get from the wealth.
Sample Input
568
Sample Output
1 13 33 4
Hint
There are huge tests, so you should refine your algorithm.题意:有n个钱,然后要平分,即每一份的数量都相同,问有多少种分法,每份最多多少钱
思路:转换为求n的因子个数和最小的因子问题,用快速求一个数因子个数的方法
一个数n = p1^a1*p2^a2...pk^ak,那么他的因子个数为(a1+1)*(a2+1)*(a3+1)*...*(ak+1),为了快速,先打一个素数表
总结:写了一发超时,然后发现了这个方法。。记住了
ac代码:
#include<stdio.h> #include<math.h> #include<string.h> #include<stack> #include<queue> #include<vector> #include<iostream> #include<algorithm> #define MAXN 1010000 #define LL long long #define ll __int64 #define INF 0xfffffff #define mem(x) memset(x,0,sizeof(x)) #define PI acos(-1) #define mod 1000000007 using namespace std; int v[MAXN]; int prime[MAXN]; void isprime() { int i,j; mem(v); v[1]=1; int cnt=0; for(i=2;i<MAXN;i++) { if(v[i]==0) { prime[cnt++]=i; for(j=i*2;j<MAXN;j+=i) v[j]=1; } } } int main() { isprime(); int t,n,m,i,j,num; int cas=0; // for(i=0;i<=5;i++) // printf("%d %d\n",v[prime[i]],prime[i]); // printf("\n"); while(scanf("%d",&n)!=EOF) { int bz=0; int ans; int nn=n; int cnt=1; for(i=0;prime[i]*prime[i]<=n;i++) { if(n%prime[i]==0) { if(bz==0) { bz=1; ans=prime[i]; } num=0; while(n%prime[i]==0) { n/=prime[i]; num++; } cnt=cnt*(num+1); } if(n==1) break; } if(n>1) cnt=cnt*2; if(v[nn]==0) printf("1 1\n"); else printf("%d %d\n",cnt-1,nn/ans); } return 0; }
相关文章推荐
- 查找目录下的所有文件中是否含有某个字符串
- iOS在SDK中打开其他接入应用的解决方案
- 1053: [HAOI2007]反素数ant 打表
- Xargs用法详解
- 【SE】Week17 : 软件工程课程总结
- 无限极分类,传递一个父ID,返回所有子集
- Linux中DNS配置和应用
- linux网络编程之socket(六):利用recv和readn函数实现readline函数
- HDOJ 2138-How many prime numbers
- 195,UITableView实例,展现其属性或方法
- 大学毕业后坚持学习有什么用
- A. Bulbs
- 判断一个字符串是否回文
- Obejct-C 字典
- 调用 LoadLibraryEx 失败
- RDD的属性
- java ssh整合出的错java.lang.NoSuchMethodError: antlr.collections.AST.getLine()
- linux服务器 磁盘和文件系统管理(一)管理磁盘及分区
- UIImage优化
- How to append list to second list