Backward Digit Sums
3 1 2 4
4 3 6
7 9
16
Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ's mental arithmetic capabilities.
Write a program to help FJ play the game and keep up with the cows.
Input
Output
sum. If there are multiple solutions, choose the one that is
lexicographically least, i.e., that puts smaller numbers first.
Sample Input
4 16
Sample Output
3 1 2 4
Hint
There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.
#include <iostream>
#include <string> using namespace std;
int n,d,vis[],num[],flag = ;
int csum();
void print();
void dfs(int k)
{
if(flag)return;
if(k > n)
{
// cout<<csum()<<endl;
if(csum() == d)
{
print();
flag = ;
return;
}
}
for(int i = ;i <= n;i ++)
{
if(vis[i] == )
{
vis[i] = ;
num[k] = i;
dfs(k + );
vis[i] = ;
}
}
}
void print()
{
cout<<num[];
for(int i = ;i <= n;i ++)
cout<<' '<<num[i];
}
int counti(int k)
{
int c = ;
for(int i = ;i < k;i ++)
c *= (n - - i);
for(int i = ;i < k;i ++)
c /= (i + );
return c;
}
int csum()
{
int sum = ;
for(int i = ;i <= n;i ++)
{
sum += num[i] * counti(i - );
}
return sum;
}
int main()
{
cin>>n>>d;
dfs();
}
Backward Digit Sums的更多相关文章
- BZOJ1653: [Usaco2006 Feb]Backward Digit Sums
1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 207 Solved: ...
- Backward Digit Sums(POJ 3187)
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5495 Accepted: 31 ...
- Backward Digit Sums(暴力)
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5664 Accepted: 32 ...
- 1653: [Usaco2006 Feb]Backward Digit Sums
1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 285 Solved: ...
- POJ3187 Backward Digit Sums 【暴搜】
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4487 Accepted: 25 ...
- POJ 3187 Backward Digit Sums 枚举水~
POJ 3187 Backward Digit Sums http://poj.org/problem?id=3187 题目大意: 给你一个原始的数字序列: 3 1 2 4 他可以相邻 ...
- 【POJ - 3187】Backward Digit Sums(搜索)
-->Backward Digit Sums 直接写中文了 Descriptions: FJ 和 他的奶牛们在玩一个心理游戏.他们以某种方式写下1至N的数字(1<=N<=10). 然 ...
- P1118 [USACO06FEB]Backward Digit Sums G/S
P1118 [USACO06FEB]Backward Digit Sums G/S 题解: (1)暴力法.对1-N这N个数做从小到大的全排列,对每个全排列进行三角形的计算,判断是否等于N. 对每个 ...
- (DFS、全排列)POJ-3187 Backward Digit Sums
题目地址 简要题意: 输入两个数n和m,分别表示给你1--n这些整数,将他们按一定顺序摆成一行,按照杨辉三角的计算方式进行求和,求使他们求到最后时结果等于m的排列中字典序最小的一种. 思路分析: 不难 ...
- Enum:Backward Digit Sums(POJ 3187)
反过来推 题目大意:就是农夫和这只牛又杠上了(怎么老是牛啊,能换点花样吗),给出一行数(从1到N),按杨辉三角的形式叠加到最后,可以得到一个数,现在反过来问你,如果我给你这个数,你找出一开始的序列(可 ...
随机推荐
- Centos 解决 No package htop available.
yum install -y epel-release 之后就可以安装 yum install -y htop 什么是EPEL? EPEL的全称叫 Extra Packages for Enterpr ...
- vc++获取系统网卡列表及IP地址信息
#include "stdafx.h" #include <Windows.h> #include <IPHlpApi.h> #include <io ...
- springboot统一异常处理及返回数据的处理
一.返回code数据的处理 代码: Result.java /** * http请求返回的最外层对象 * Created by 廖师兄 * 2017-01-21 13:34 */ public cla ...
- mysql 不同条件count ,多条件count()
create table abc(A int,B int) Select A,count(B) as total from ABC group by A Select A,count(B) as to ...
- 阿里云centOS7.4 ftp连接不上的问题
首先查看是开启21端口 选择ECS-->安全组-->配置规则 增加21端口配置 配置如下
- unity调用Android百度地图
由于个人是Android小白,在这个配置上面被折磨了很久,因此写下这篇文章 工具:eclipse + unity5.6.1 首先去百度地图开发者平台下载你需要的资源,我只需要显示地图和定位,这个时候你 ...
- CSS3:@font-face规则
前言 过去,Web设计师为了保证网站能够正常显示,只能使用“Web安全字体”,即每台机器都预装的字体.但Web安全字体有时并不好看... @font-face能够使得任何一台机器能够显示理想中的字体. ...
- nginx和php-fpm的启停和配置
一.nginx的启停 (1) 启动nginx /etc/init.d/nginx start (2) 停止nginx /etc/init.d/nginx stop (3) 重启nginx /etc/i ...
- spring配置bean的生命周期
配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...
- CentOS7 LDAP 2.4 安装配置
软件安装 # yum -y install openldap-servers openldap-clients # systemctl start slapd # systemctl enable s ...