zoukankan      html  css  js  c++  java
  • 【学习笔记】〖九度OJ〗题目1104:整除问题

    题目1104:整除问题

    时间限制:1 秒

    内存限制:32 兆

    特殊判题:

    提交:1945

    解决:607

    题目描述:

    给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除。

    输入:

    两个整数n(2<=n<=1000),a(2<=a<=1000)

    输出:

    一个整数.

    样例输入:
    6 10
    样例输出:
    1
    来源:
    2011年上海交通大学计算机研究生机试真题
    答疑:
    解题遇到问题?分享解题心得?讨论本题请访问:http://t.jobdu.com/thread-7827-1-1.html
    本题涉及数据过大,所以用分解质因子做,如果a能整除b,那么a的每个质因子的次数都要大于b对应质因子的次数


    #include<iostream>
    #include<cmath>
    using namespace std;
    
    int primef[1001];//质因子个数
    int primea[1001];
    int main()
    {
    	freopen("input.in", "r", stdin);
    	freopen("output.out","w", stdout);
    
    	int n, a;
    
    	while (cin >> n>>a)
    	{
    		int i,j;
    		//清空
    		for (i=0; i<1001; i++)
    		{
    			primef[i]=0;
    			primea[i]=0;
    		}
    		int max;//循环条件
    		//对n!分解质因子
    		for(i=n; i>=0; i--)
    		{
    			bool isOver = false;
    			int temp = i;
    			while(!isOver)
    			{
    				max = sqrt((double)temp);
    				for (j=2; j<=max; j++)
    				{
    					if (temp%j == 0)
    					{
    						primef[j]++;
    						temp /= j;
    						break;
    					}
    				}
    				if (j>max)
    				{
    					isOver = true;
    					if(temp>1)
    					{
    						primef[temp]++;
    					}
    				}
    			}
    		}
    
    		//对a分解质因子
    		int tmp = a;
    		bool isOver = false;
    		while(!isOver)
    		{
    			
    			max = sqrt((double)tmp);
    			for (j=2; j<=max; j++)
    			{
    				
    				if (tmp%j == 0)
    				{
    					primea[j]++;
    					tmp /= j;
    					break;
    				}
    			}
    			if (j>max)
    			{
    				if(tmp>1)
    				{
    					primea[tmp]++;
    				}
    				isOver = true;
    			}
    		}
    		//寻找最小次数
    		int minIndex = 1000;
    		for (i=2; i<=a; i++)
    		{
    			if (primea[i]!=0
    				&&primef[i]/primea[i] <minIndex)
    			{
    				minIndex = primef[i]/primea[i];
    			}
    		}
    
    		cout << minIndex << endl;
    	}
    	return 0;
    }


  • 相关阅读:
    程序的编写/数据结构和操作/容器的应用/查询程序
    c++ 输入流
    转 中断和事件
    库函数, string , integer to char
    转义字符 / ascll表
    notepad change background color
    PlayMark视频教程
    unity3d webplayer 16:9 居中显示模板
    Unity3d 组件设计的思考[转]
    读取到系统字体
  • 原文地址:https://www.cnblogs.com/ymjia/p/3590298.html
Copyright © 2011-2022 走看看