2016年第七届 蓝桥杯 全国总决赛B题(完全平方数)
2016-06-03 22:44
736 查看
题目意思就是:
给你0,1,2,3,4,5,6,7,8,9十个数字,要你选出任意一个或几个组合在一起成为完全平方数,每个数字都必须选且只能选一次,求可能的方案。
比如有其中几种符合题意的情况:
0 16 25 73984
0 1 625 73984
0 4 16 537289
0 16 784 5329
0 25 784 1936
ans=300;
给你0,1,2,3,4,5,6,7,8,9十个数字,要你选出任意一个或几个组合在一起成为完全平方数,每个数字都必须选且只能选一次,求可能的方案。
比如有其中几种符合题意的情况:
0 16 25 73984
0 1 625 73984
0 4 16 537289
0 16 784 5329
0 25 784 1936
ans=300;
#include<iostream> using namespace std; #include<cstring> #include<cstdio> #include<vector> #include<algorithm> #include<cmath> typedef long long ll; const ll inf=(ll)(0x3f3f3f3f); int ans=0; ll t[105]; vector<ll> vec[500]; int len=0; ll b[15]; int lens[600]; const int maxn=900005; ll pri[maxn]; bool judge(ll x) { ll o1=(ll)(sqrt(x)); return o1*o1==x; } void solve(ll *a,int num) { memset(b,inf,sizeof(b)); int u=0,pos=0; bool is=true; for(int i=0; i<num; ++i) { ll s=0; if(!a[pos]&&t[i]>1) { is=false; break; } for(int j=pos; j<t[i]+pos; ++j) { s=s*10+a[j]; } b[u]=s; ++u; pos=t[i]+pos; } if(is) { sort(b,b+u); bool issame=true; for(int i=0; i<len; ++i) { int sj=lens[i]; if(sj!=u)continue; bool same1=true; for(int j=sj-1; j>=0; --j) { if(vec[i][j]!=b[j]) { same1=false; break; } } if(same1) { issame=false; break; } }//判重 if(issame) { ++len; lens[len-1]=u; for(int i=0; i<u; ++i) { vec[len-1].push_back(b[i]); // cout<<b[i]<<" "; } // cout<<endl; ++ans; } } } ll ppp=(ll)maxn; void dfs(ll *a,int sum,int num,int pos) { if(!sum) { solve(a,num); } else if(sum<0); else { for(int i=sum; i>=1; --i) {//求划分数 t[num]=i; ll ts=0; bool can=true; if(i>1&&a[pos]==0)continue; for(int j=pos; j<pos+i&&can; ++j) { ts=ts*10+a[j]; } if((ts<ppp&&(!pri[ts]))||!judge(ts))continue;//剪枝 dfs(a,sum-i,num+1,pos+i); } } } int main() { ll a[]= {0,1,2,3,4,5,6,7,8,9}; for(int i=0; i<500; ++i) vec[i].clear(); for(ll i=0; i<ppp; ++i) { pri[i]=judge(i); } int op=0; do { dfs(a,10,0,0); } while(next_permutation(a,a+10)); cout<<ans<<endl; return 0; }
相关文章推荐
- c++6.1
- 链表的逆置
- C语言中字符串测试函数总结
- Winform开发框架之读卡器和条码扫描枪的数据接收处理
- 修改主机名
- 深入浅出Node.js(一):什么是Node.js
- 《软件工程》个人最终总结
- 友元函数
- JAVA之旅(十二)——Thread,run和start的特点,线程运行状态,获取线程对象和名称,多线程实例演示,使用Runnable接口
- c++6
- 数据库之乐观锁和悲观锁篇
- JAVA之旅(十二)——Thread,run和start的特点,线程运行状态,获取线程对象和名称,多线程实例演示,使用Runnable接口
- 通过GitHub Pages建立个人站点(详细步骤)
- <LeetCode OJ> 106. Construct Binary Tree from Inorder and Postorder Traversal
- oracle PL/SQL语句
- 51NOD-1670 打怪兽(排列组合)
- win7安装plsql连接oracle11g 11.2.0.4 64位数据库的简单方式
- 程序员如何谋划出月薪3万
- CURL常用命令
- NDIS Filter Study