PE 118-119 (dfs+暴力)

PE 118

题解:next_permutation()+dfs就可以了

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int ans=0;
vector<int>v;
int isprime(ll n)
{
	if(n==1)return 0;
	for(ll i=2;i*i<=n;i++){
		if(n%i==0)return 0;
	}
	return 1;
}
void dfs(int pos,ll now){
	if(pos>=9){
		if(ans<=10){
			//for(int i=0;i<9;i++)
			//cout<<v[i]<<" ";
			//cout<<endl;
		}
		
		ans++;return; 
	}
	ll n = 0;
	while(pos < 9){
		n *= 10;
		n += v[pos];
		if(isprime(n) && n >= now){
			dfs(pos+1,n);
		}
		pos++;
	}
}
int main()
{
	for(int i=1;i<=9;i++){v.push_back(i);}
	do{
		dfs(0,0);
	}while(next_permutation(v.begin(),v.end()));
	cout<<"ans="<<ans<<endl;
	return 0;
}
PE 119

题解:暴力....

代码:

#include<bits/stdc++.h>
using namespace std;
long long gao(int m,int n)
{
	long long  res=(long long)pow(m,n);
	long long ans=0;
	while(res)
	{
		ans+=res%10;
		res/=10;
	}
	return ans;
}
set<long long>s;
int main()
{
	
	for(int i=1;i<=100;i++)
	{
		for(int j=1;j<=100;j++)
		{
			if((long long)gao(j,i)==(long long)j)
			{
			    s.insert(pow(j,i));
			}
		}
	}
	int num=1;
	for(set<long long>::iterator it = s.begin();it !=s.end();it++)
	{
		if(*it>=10)
		cout<<num++<<":"<<*it<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值