(Problem 29)Distinct powers
Consider all integer combinations ofabfor 2a5 and 2b5:
22=4, 23=8, 24=16, 25=32
32=9, 33=27, 34=81, 35=243
42=16, 43=64, 44=256, 45=1024
52=25, 53=125, 54=625, 55=3125
If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
How many distinct terms are in the sequence generated byabfor 2a100 and 2b100?
题目大意:
考虑 ab 在 2 a 5,2 b 5下的所有整数组合:
22=4, 23=8, 24=16, 25=32
32=9, 33=27, 34=81, 35=243
42=16, 43=64, 44=256, 45=1024
52=25, 53=125, 54=625, 55=3125
如果将这些数字排序,并去除重复的,我们得到如下15个数字的序列:
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
ab 在 2 a 100,2 b 100 下生成的序列中有多少个不同的项?
算法设计(方法1):
1、将ab 进行因数分解,以字符串的形式保存,eg. 285 = (4 * 7)5 = (22 * 7)5 = 2^10*7^5
2、用一个结构体数组保存所有的数的因数分解表达式
3、对上述结构体数组排序
4、遍历此数组,找出不相同的项的总数
//(Problem 29)Distinct powers
// Completed on Tue, 19 Nov 2013, 07:28
// Language: C
//
// 版权所有(C)acutus (mail: acutus@126.com)
// 博客地址:http://www.cnblogs.com/acutus/
#include <stdio.h>
#include <string.h> const int prim[] = {, , , , , , , , , , , ,,
, , , , , , , , , , , }; struct node
{
char list[]; }num[]; int cmp(const void *a, const void *b)
{
return strcmp((*(struct node*)a).list, (*(struct node*)b).list);
} char * explain(int a, int b) /*将a^b分解因数*/
{
char s[], ch;
char *p;
p = s;
int t;
for(int i = ; i < ; i++) {
t = ;
while(a % prim[i] == ) {
if(t == ) {
sprintf(p,"%d",prim[i]);
}
a /= prim[i];
t++;
}
if(t > ) {
p = s + strlen(s);
*p++ = '^';
t = t * b;
sprintf(p,"%d",t);
p = s + strlen(s);
if(a != ) {
*p++ = '*';
} else {
break;
}
}
}
return s;
} void solve(void)
{
int i, j, k, sum;
k = ;
for(i = ; i < ; i++) {
for(j = ; j < ; j++) {
strcpy(num[k++].list, explain(i,j));
}
}
qsort(num, , sizeof(num[]),cmp);
sum = ;
for(i = ; i < ; ) {
j = i + ;
if(j >= ) break;
while(strcmp(num[i].list, num[j].list) == ) {
j++;
}
i = j;
sum ++;
}
printf("%d\n",sum);
} int main(void)
{
solve();
return ;
}
算法设计(方法2):
仔细考察数字矩阵的规律,可以发现:
能够发生重复的数字,将他们因数分解以后,得到的指数的底都是相同的,e.g. 16与64……,在2~100中,能够发生重复数字的底只有4、8、16、32、64、9、27、81、25、36、49、81、100,于是可以在底为2的时候就排除掉以4、8、16、32、64为底的重复的数字。
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h> #define N 101
#define M 601 int main(void)
{
int answer = ;
int i, j, k, l;
bool flag[M]; bool use[N] = {false}; for (i = ; i < N; i++)
{
if (!use[i])
{
int t = i; memset(flag, false, sizeof(flag)); for (j = ; j < N; j++)
{
t = t * i;
if (t >= N)
{
break;
}
use[t] = true;
} for (k = ; k < j; k++)
{
for (l = ; l < N; l++)
{
flag[k*l] = true;
}
} for (k = ; k < M; k++)
{
if(flag[k]){
answer++;
} }
}
}
printf("%d\n",answer);
return ;
}
Answer:
|
9183 |
(Problem 29)Distinct powers的更多相关文章
- (Problem 47)Distinct primes factors
The first two consecutive numbers to have two distinct prime factors are: 14 = 2 7 15 = 3 5 The fi ...
- (Problem 53)Combinatoric selections
There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 2 ...
- (Problem 57)Square root convergents
It is possible to show that the square root of two can be expressed as an infinite continued fractio ...
- (Problem 73)Counting fractions in a range
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...
- (Problem 42)Coded triangle numbers
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...
- (Problem 41)Pandigital prime
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...
- (Problem 70)Totient permutation
Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number ...
- (Problem 74)Digit factorial chains
The number 145 is well known for the property that the sum of the factorial of its digits is equal t ...
- (Problem 46)Goldbach's other conjecture
It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a ...
随机推荐
- libmemcached安装及简单例子
libmemcached安装及简单例子 1.下载安装libmemcached $ wget http://launchpad.net/libmemcached/1.0/0.44/+download/ ...
- 夜未央Test1
积木游戏(block.pas) [题目描述] 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,最高的积木的最终需要达到h. 在搭建开始之前,没有任何积木(可以看成n ...
- ios中的银联支付
场景 随着移动互联网的迅猛发展,移动互联已经深深地融入我们的生活.其中,支付方式也是我们生活中经常遇到的情况.所以,在我们的应用中加入支付功能是多么的重要.现在主流的支付接口,一是支付宝类的,一是银联 ...
- 【hadoop】14、hadoop2.5的mapreduce的 配置
配置mapreduce <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href ...
- VIM在文件夹中查找
在vim中提供2中方法来在其他文件或者文件夹中搜索字符串,第一种是vimgrep还有一种是grep. 如果只是在当前打开的文件中查找字符串的,使用 :? 后面加上想要搜索的字符串就可以. 这里要解决的 ...
- JavaSE复习日记 : 算是个小前言吧
/* * Java也学了好久了,抽个时间整理了一下课堂笔记,也有些是我刚开始学会犯的一些错误.在这里浅谈一下JavaSE的基础内容,对我来说也是一种不错的复习方式. * * 那好,对于初学者来说,学习 ...
- C++面向对象类的书写相关细节梳理
类的问题 继承类的原因:为了添加或者替换功能. 1. 继承时重写类的方法 v 替换功能 ① 将所有方法都设置为virtual(虚函数),以防万一. Virtual:经验表明最好将所有方法都设置为vir ...
- Android应用开发基础篇(11)-----ViewFlipper
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/01/2376067.html 一.概述 ViewFlipper这个部件是用来实现多页显示的,多页 ...
- struts的由来
当学习或工作时,有些同学会谈到熟悉struts.hibernate.spring等等框架,貌似熟悉这些框架是精通java的表现,但是我们应该首先弄明白为什么要学框架?是为了学习而学习?还是为了工作而学 ...
- ES6学习笔记:Module的基本用法
export和import ES6实现了模块功能,试图解决JavaScript代码上的依赖和部署上的问题,取代现有的CommonJs的AMD规范,成为浏览器和服务器通用的模块解决方案. 模块功能有两个 ...