Problem A. Arithmetic Derivative-XVII Open Cup named after E.V. Pankratiev||找规律&数学&DFS

题目描述:


签到题,找规律,

手推了规律如下:


AC代码:

/*
2017年8月29日22:33:28
Problem A. Arithmetic Derivative
AC 找规律+简单DFS+溢出处理 

*/
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector> 
using namespace std;

typedef long long ll;
const ll inf=2e18;
//讲满足k=1的前6个数存下来 用来搜索 
ll p[]={4,27,3125,823543,285311670611,302875106592253}; 
ll cur,k,r;//当前层数的值,需要搜索的层数,上界 
ll ans[1000]; 

set<ll> st;//用set是因为题目要求排序+去重 
set<ll>::iterator it;

void dfs(ll num,ll cur){
	if(num==k){
		for(int i=0;i<6;i++){
			ll tmp=cur*p[i];
			//这里是对溢出的处理  
			/*
			如果不满足条件也就是说 p[i]*cur 会大于INF,那么数据会发生溢出
			溢出的数据不满足条件,舍去 
			*/ 
			if((inf/cur)>=p[i]){
				/*不发生溢出再考虑与r的大小*/ 
				if(tmp<=r) st.insert(tmp);
				else return;
			}
			else return;
		}
	}
	else if(num<k){
		for(int i=0;i<6;i++){
			if((inf/cur)>=p[i])
				dfs(num+1,cur*p[i]);
			else return;
		}
	}
}

int main(){
	scanf("%lld%lld",&k,&r);
	dfs(1,1);
	printf("%lld\n",st.size());
	for(it=st.begin();it!=st.end();it++){
		if(it!=st.begin()) printf(" ");
		printf("%lld",*it);
	}printf("\n");
	return 0;
} 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值