B. MooBuzz

GDUT 2020寒假训练 排位赛一 B

原题链接

题目

原题截图
Farmer John’s cows have recently become fans of playing a simple number game called “FizzBuzz”. The rules of the game are simple: standing in a circle, the cows sequentially count upward from one, each cow saying a single number when it is her turn. If a cow ever reaches a multiple of 3, however, she should say “Fizz” instead of that number. If a cow reaches a multiple of 5, she should say “Buzz” instead of that number. If a cow reaches a multiple of 15, she should say “FizzBuzz” instead of that number. A transcript of the first part of a game is therefore: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16

Having a slightly more limited vocabulary, the version of FizzBuzz played by the cows involves saying “Moo” instead of Fizz, Buzz, and FizzBuzz. The beginning of the cow version of the game is therefore

1, 2, Moo, 4, Moo, Moo, 7, 8, Moo, Moo, 11, Moo, 13, 14, Moo, 16

Given N (1≤N≤109), please determine the Nth number spoken in this game.

Test cases 2-5 satisfy N≤106.

Input
The input consists of a single integer, N.

Output
Please print out the Nth number spoken during the game.

题目大意

输入n 输出第n个有效的数字(逢3和5的倍数不是有效的数字)

思路

刚开始第一想法是线性筛,就跟线性筛素数那样,筛掉3和5 的倍数,但是筛数的算法要建立至少1e9长的数组,而且时间复杂度肯定超了,所以在想怎么优化筛法。
想了好久没有好的筛数的思路,那么考虑到1e9的数据量再想会不会是数学题,以15的倍数为界限,每逢一个15的倍数,前面都有8个消失的数字(就是3和5的倍数)。那么比如当我们询问第19个数是什么,19/8=2,也就是说前面有2组15,那么每一组15有15个数,但是少了7个无效的数字,也就是有215-27=16个数字,也就是截止30,也只有16个数字报出来,那么我们以30为新的起点继续模拟一组,直到16累加到19,输出当前的数。
(当时做这道题卡了很久后面有点慌,后来觉得这个算法不太对劲,直到看到了其他人都是用的二分。。。)
那么二分的思路是,首先,我询问x是第几个报的数,我能得到x-x/3-x/5+x/15,那么这就好说了,从1~1e9二分答案,直到返回的这个答案是我们题目所询问的n的时候输出这个x(二分好简单)

代码

朴素的验证代码

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
//int num[1874999999]; 
int main()
{
	long n;
	n=10;
again:
	//cin>>n;
	cout<<n<<" ";
	long cnt=0,i;
	for(i=1;cnt<n;i++)
	{
		if(i%3==0||i%5==0)continue;
		cnt++;
	}
	cout<<i-1<<endl;
	n++;
	if(n==100)return 0;
	goto again;
	return 0;
}

最后的提交

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
//int num[1874999999]; 
int main()
{
	long n;
//	n=10;
//again:
	
	cin>>n;
	int n7=n/8;
	//cout<<n<<" ";
	int base=n7*15-n7*7;
	long cnt=0,i;
	cnt=base;
	i=n7*15;
	for(i;cnt<n;i++)
	{
		if(i%3==0||i%5==0)continue;
		cnt++;
	}
	cout<<i-1<<endl;
	//n++;
	//if(n==100)return 0;
//goto again;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值