XTU 1183 Factors

Factors

Accepted : 9   Submit : 65
Time Limit : 1000 MS   Memory Limit : 65536 KB

题目描述

从前有个叫天天的熊孩子,他非常喜欢数字1。为什么呢?因为一生二,二生三,三生万物,由此熊孩子认为1是万物之始。现在天天碰到一个问题:有一个正整数集合,天天需要把所有数都变成1。 每一次操作,天天可以找一个素数X,让集合中的每一个数都移除(移除等同于除尽)这个素因子(若某数不包含素因子X,则该数不变)。如果这个时候集合内所有数都为1,则结束操作。否则,把集合中所有大于1的数乘以在素数表中排在X之后的那个素数(素数表按从小到大排序,素数表是所有的素数的集合)。天天讨厌麻烦,所以他想知道最少几次操作可以把整个集合中每个数都变成1。

输入

多组数据。每组数据有两行,第一行是一个数n(0< n ≤10000)表示集合中有多少个数,如果n为0,表示输入结束,这组样例不需要处理。 第二行是集合中n个数(0< 集合中每个数≤105

输出

每组数据输出一行包括最少的操作次数。

样例输入

3
2 3 6
4
1 1 1 1
0

样例输出

2
0

线索

第一个例子先取x=2,那么集合内每个元素去掉2的因子后为{1,3,3},然后乘以2后面的下一个素数3,新集合为{1,9,9},然后再选x=3,集合变为{1,1,1}。

Source

XTU OnlineJudge

思路:打出素数表,计算出输入数据中包含的最大素数和最小素数,然后最大素数与最小素数之间的素数的个数加1就是答案了。

#include<stdio.h>
#include<string.h>
const int MAXN=110005;//求出素数要包括大于100000的第一个素数
bool vis[MAXN];
long long prime[MAXN/5];
int tot=0;
void getPrime()
{
    for(long long i=2;i<MAXN;i++)
        if(!vis[i])
        {
            prime[tot++]=i;
            for(long long j=i*i;j<MAXN;j+=i) vis[j]=true;
        }
}
int arr[10005];
int b[100005];
int main(){
	int t;
	getPrime();
	//printf("%d\n",tot);
	while(scanf("%d",&t)&&t!=0){
		memset(b,0,sizeof(b));
		int max=0;//记录最大的素数
		int min=MAXN;//记录最小的素数
		for(int i=0;i<t;i++) scanf("%d",&arr[i]);
		for(int j=0;j<t;j++){
			int n=arr[j];
			int k=0;
			for(int i=0;prime[i]*prime[i]<=n;i++){
				if(n%prime[i]==0){
					if(prime[i]>max) max=prime[i];
					if(prime[i]<min) min=prime[i];
					while(n%prime[i]==0){
						k++;
						n/=prime[i];
					}
					if(k>b[prime[i]]) b[prime[i]]=k;
				}
			}
			if(n!=1){
				b[n]=1;
				if(n>max) max=n;
				if(n<min) min=n;
			}
		}
		int ans=0;
		//for(int i=0;i<1005;i++) ans+=b[i];
		int x=0,y=0;
		for(int i=0;i<tot;i++){
			if(prime[i]==min){
				x=i;
			}
			if(prime[i]==max){
				y=i;
				//printf("max=%d\n",max);
				break;
			}
		}
		if(max==0) ans=0;//对全是1的情况进行特判
		else ans=y-x+1;//记算最大素数和最小之间的素数个数加1
		printf("%d\n",ans);
	}
	return 0;
}
时间: 2024-10-20 04:16:43

XTU 1183 Factors的相关文章

xtu 1242 Yada Number 打表

Yada Number       Time Limit : 2000 MS   Memory Limit : 65536 KB Yada Number Problem Description: Every positive integer can be expressed by multiplication of prime integers. Duoxida says an integer is a yada number if the total amount of 2,3,5,7,11,

[CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字

7.7 Design an algorithm to find the kth number such that the only prime factors are 3,5, and 7. 这道题跟之前LeetCode的那道Ugly Number II 丑陋数之二基本没有啥区别,具体讲解可参见那篇,代码如下: class Solution { public: int getKthMagicNumber(int k) { vector<int> res(1, 1); int i3 = 0, i

xtu summer individual 6 B - Number Busters

Number Busters Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 382B64-bit integer IO format: %I64d      Java class name: (Any) Arthur and Alexander are number busters. Today they've got a competition.

xtu DP Training C.炮兵阵地

炮兵阵地 Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 118564-bit integer IO format: %lld      Java class name: Main 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用"P"表

xtu summer individual 1 E - Palindromic Numbers

E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In this

1096. Consecutive Factors (20)

Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3*5*6*7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maxim

XTU 1242 Yada Number 容斥

Yada Number Problem Description: Every positive integer can be expressed by multiplication of prime integers. Duoxida says an integer is a yada number if the total amount of 2,3,5,7,11,13 in its prime factors is even. For instance, 18=2 * 3 * 3 is no

PAT 1059. Prime Factors (25)

1059. Prime Factors (25) Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km. Input Specification: Each input file contains one test case which gives a positive inte

1059. Prime Factors (25)

时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1* p2^k2 *…*pm^km. Input Specification: Each input file contain