Prime Land

Description

Everybody in the Prime Land is using a prime base number system. In this system, each positive integer x is represented as follows: Let {pi}i=0,1,2,... denote the increasing sequence of all prime numbers. We know that x > 1 can be represented in only one way in the form of product of powers of prime factors. This implies that there is an integer kx and uniquely determined integers ekx, ekx-1, ..., e1, e0, (ekx > 0), that The sequence

(ekx, ekx-1, ... ,e1, e0)



is considered to be the representation of x in prime base number system.

It is really true that all numerical calculations in prime base number system can seem to us a little bit unusual, or even hard. In fact, the children in Prime Land learn to add to subtract numbers several years. On the other hand, multiplication and division is very simple.

Recently, somebody has returned from a holiday in the Computer Land where small smart things called computers have been used. It has turned out that they could be used to make addition and subtraction in prime base number system much easier. It has been decided to make an experiment and let a computer to do the operation ``minus one''.

Help people in the Prime Land and write a corresponding program.

For practical reasons we will write here the prime base representation as a sequence of such pi and ei from the prime base representation above for which ei > 0. We will keep decreasing order with regard to pi.

Input

The input consists of lines (at least one) each of which except the last contains prime base representation of just one positive integer greater than 2 and less or equal 32767. All numbers in the line are separated by one space. The last line contains number 0.

Output

The output contains one line for each but the last line of the input. If x is a positive integer contained in a line of the input, the line in the output will contain x - 1 in prime base representation. All numbers in the line are separated by one space. There is no line in the output corresponding to the last ``'' line of the input.

Sample Input

17 1
5 1 2 1
509 1 59 1
0

Sample Output

2 4
3 2
13 1 11 1 7 1 5 1 3 1 2 1
解题思路://反复试除,从可能的最大的素数一直往下查找。 例:输入:5 1 2 1 则x=5^1*2^1=10,所以x-1=9,9可以表示成:9=3^2               输出:3 2
获得素数的算法(我写了两种,其中一种较精炼,就是从i=2开始找i的倍数,再i++,直到M就可以查找完了。
#include<stdio.h>
#include<math.h>
#include<string.h>
const int MAX=33333;
int prime[MAX],num;
bool is[MAX];
/*int getpri()//获得素数
{
	int i,j,k=0,d;
	is[1]=true;
	is[2]=false;
	for(i=2;i<=(int)sqrt((double)MAX+0.0);i++)
	{
		d=1;
		if(is[i]==false)
		{
			for(j=2;d<33333;j++)
			{
				d=i*j;
				is[d]=true;
				if(d>=33333)
					break;
			}
		}
	}
	for(i=2;i<33333;i++)
	{
		if(is[i]==false)
			prime[k++]=i;
	}
	return k;
}*/
int getpri()
{
	int i,j,k=0;
	memset(is,true,sizeof(is));
	for(i=2;i<=MAX;i++)
	{
		if(is[i])
		{
			prime[k++]=i;
           for(j=i+i;j<MAX;j+=i)
			   is[j]=false;
		}
	}
	return k;
}
void fun(int x)  //函数处理。
{
   int i,cnt=0;
   bool frist=true;
   for(i=num-1;i>=0;--i)
   {
	   cnt=0;
       while(x%prime[i]==0)
	   {
		   cnt++;
		   x/=prime[i];
	   }
	   if(cnt)
	   {
		   if(frist)
		   {
			   printf("%d %d",prime[i],cnt);
			   frist=false;
		   }
		   else
			   printf(" %d %d",prime[i],cnt);
	   }
   }
   printf("\n");
}
int main()
{
	int p,e;
	num=getpri();
	while(true) //循环求值。
	{
		int sum=1;
		while(true)
		{
			scanf("%d",&p);
			if(p==0)
				return 0;
			scanf("%d",&e);
             sum*=(int)pow((double)p,e);//sum *= (int)pow(p,e);没有double提交就会出现编译错误。
			 if(getchar()=='\n')//出现回车键就结束输入。
				 break;
		}
		fun(sum-1);  //处理数据。
	}
	return 0;
}

% 参数设置 grid_size = 50; % 500m 10m land_size = 500; tree_area = 10; safety_radius = 2.5; heights = [5, 10, 15, 20, 25]; canopy_radius = [2.8, 5.5, 8.5, 11.9, 14.5]; % 定义最大树木数目 maximum_trees = grid_size^2; % 网格中最多能种植的树木数目 % 输入已经种植的树木数目 N_prime = input('已经种植的树木数目: '); % 初始化变量 x = zeros(grid_size, grid_size); h = ones(grid_size, grid_size) * 5; % 假设所有树的初始高度为5米 % 初始化总成本 total_cost = 0; % 遍历网格 for i = 1:grid_size for j = 1:grid_size % 检查安全距离 safe = true; for k = max(1, i-1):min(grid_size, i+1) for l = max(1, j-1):min(grid_size, j+1) if i ~= k || j ~= l if sqrt((i-k)^2 + (j-l)^2) * tree_area < 2 * safety_radius safe = false; break; end end end if ~safe break; end end % 如果满足安全距离条件,尝试种植树木 if safe && N_prime < maximum_trees x(i, j) = 1; % 计算最佳树高 min_cost = inf; best_height = 0; for height = heights canopy_r = interp1(heights, canopy_radius, height); if (i-1) * tree_area + canopy_r <= land_size && (j-1) * tree_area + canopy_r <= land_size cost = 10 * height + 10; if cost < min_cost min_cost = cost; best_height = height; end end end h(i, j) = best_height; N_prime = N_prime + 1; % 更新已种植的树木数目 end end end % 计算结果 remaining_trees = maximum_trees - N_prime; % 在已经种植的树木基础上还能种植的树木数目 total_cost = sum(sum((h * 10 + 10) .* x)); fprintf('在已经种植的树木基础上还能种植的树木数目: %d\n', remaining_trees); fprintf('总成本: %d\n', total_cost);请分析一下此代码的错误
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值