URAL 2047 Maths 打表 递推
Maths
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87157#problem/B
Description
Input
Output
If there is no such sequence output “Impossible”. Otherwise output space-separated integers a1, …, an (1 ≤ ai ≤ 300).
Sample Input
3
Sample Output
1 3 4
HINT
题意
让你构造n个数,要求a1+……+ak的和的因子数恰好为ak
题解:
假设我们已经构造出了ak,且前缀和sum已知,那么ak-1=sum-ak,这个是显然的结论
于是我们直接打表打出来100000位就好了,然后把sum求出来,然后前面直接递推就好了
代码:
打表程序:
#pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 2000001
#define mod 1000000007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** int cnt[*];
vector<int> Q;
int flag=;
int n;
void dfs(int N,int sum,int z)
{
if(flag)
return;
if(sum+z>*)
return;
if(N>&&cnt[sum]!=z)
return;
if(N==n)
{
printf("%d",Q[]);
for(int i=;i<Q.size();i++)
printf(" %d",Q[i]);
printf("\n");
cout<<sum<<endl;
flag=;
return;
}
for(int i=;i<=;i++)
{
Q.push_back(i);
dfs(N+,sum+i,i);
Q.erase(Q.end()-);
} }
int main()
{
freopen("1.out","w",stdout);
for(int i=;i<=*;i++)
{
for(int j=i;j<*;j+=i)
{
cnt[j]++;
}
}
n=read();
Q.clear();
flag=;
for(int i=;i<=;i++)
{
Q.push_back(i);
dfs(,i,i);
Q.erase(Q.end()-);
}
if(flag==)
printf("Impossible\n");
}
AC程序:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 20001
#define mod 1000000007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//**************************************************************************************
const int N=;
int cnt[N];
int a[N],sum=;
vector<int> Q;
int flag=;
int n;
int tot;
int main()
{
for(int i=;i<N;i++)
{
for(int j=i;j<N;j+=i)
cnt[j]++;
}
for(int p=,i=;i>;p-=cnt[p],i--)
{
a[i]=cnt[p];
sum+=a[i];
}
n=read();
for(int i=;i<n;i++) printf("%d ",a[i]);
printf("%d\n",a[n]);
}
URAL 2047 Maths 打表 递推的更多相关文章
- poj1338 Ugly Numbers 打表, 递推
题意:一个数的质因子能是2, 3, 5, 那么这个数是丑数. 思路: 打表或者递推. 打表: 若该数为丑数,那么一定能被2 或者3, 或者5 整除, 除完之后则为1. #include <ios ...
- URAL 1260 Nudnik Photographer(递推)
题目链接 题意 : 给你1到n这n个数,排成一排,然后1放在左边最开始,剩下的数进行排列,要求排列出来的数列必须满足任何两个相邻的数之间的差不能超过2,问你有多少种排列 思路 : 对于dp[n], n ...
- URAL 2047 Maths (数学)
对于一个数来说,它的除数是确定的,那么它的前驱也是确定的,而起点只能是1或2,所以只要类似筛法先预处理出每个数的除数个数 ,然后递推出每个数往前的延伸的链长,更新最大长度,记录对应数字.找到maxn以 ...
- 递推DP URAL 1017 Staircases
题目传送门 /* 题意:给n块砖头,问能组成多少个楼梯,楼梯至少两层,且每层至少一块砖头,层与层之间数目不能相等! 递推DP:dp[i][j] 表示总共i块砖头,最后一列的砖头数是j块的方案数 状态转 ...
- 递推DP URAL 1260 Nudnik Photographer
题目传送门 /* 递推DP: dp[i] 表示放i的方案数,最后累加前n-2的数字的方案数 */ #include <cstdio> #include <algorithm> ...
- 递推DP URAL 1353 Milliard Vasya's Function
题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...
- 递推DP URAL 1119 Metro
题目传送门 /* 题意:已知起点(1,1),终点(n,m):从一个点水平或垂直走到相邻的点距离+1,还有k个抄近道的对角线+sqrt (2.0): 递推DP:仿照JayYe,处理的很巧妙,学习:) 好 ...
- 【THUSC2017】【LOJ2981】如果奇迹有颜色 DP BM 打表 线性递推
题目大意 有一个 \(n\) 个点的环,你要用 \(m\) 中颜色染这 \(n\) 个点. 要求连续 \(m\) 个点的颜色不能是 $1 \sim m $ 的排列. 两种环相同当且仅当这两个环可以在旋 ...
- jzoj5195. 【NOIP2017提高组模拟7.3】A(递推,打表)
Description
随机推荐
- Android UncaughtExceptionHandler,捕获错误
最近在做个项目,需要在程序出现运行时异常和错误导致程序crash时进行一些操作,找到一个方法 Thread.setDefaultUncaughtExceptionHandler(new Uncaugh ...
- 自己动手写路由器之ioctl获取网络接口信息
最近打算写一个简单路由器,里面有用到ioctl获取网络接口信息,那就先把这部分单独拿出来说一说吧! ioctl这个函数,可以用来对特殊文件的基础设备参数进行操作,它们可以完成与打开文件描述符相关联的控 ...
- hdu5073 简单枚举+精度处理
其实这题还是挺简单的,因为移动k个星球后,这k个星球的权值就可以变为0,所以只有剩下的本来就是连着的才是最优解,也就是说要动也是动两端的,那么就O(N)枚举一遍动哪些就好了. 我是在杭电oj题目重现的 ...
- DateTime.ToString格式化日期,使用DateDiff方法获取日期时间的间隔数
一:DateTime.ToString格式化日期 二:代码 using System; using System.Collections.Generic; using System.Component ...
- 取消eclipse启动时的subclipse Usage弹窗
取消windows–>perferences–>general–>startup and shutdown里最下面的Subclipse Usage report 勾选即可
- Python 笔记 : 类和继承
# -*- coding= utf-8 -*- # 文件编码定义的语法规则是: coding[:=]/s*([-/w.]+) # 未指定编码将默认为 : ASCII # 同时要注意物理文件的编码也要 ...
- BS与CS的联系与区别
C/S是Client/Server的缩写.服务器通常采用高性能的PC.工作站或小型机,并采用大型数据库系统,如Oracle.Sybase.InFORMix或SQL Server.客户端需要安装专用的客 ...
- 坚持自学的第二天,bootstrap初入门
前言 昨天,初步学完了jekyll目录结构与Liquid语法的应用与认识. 日志 今天刚入门,做了一个bootstrap导航栏,但是选中状态不行,找了JS中写好的API,写法与视频中讲的有点不一样,但 ...
- 开源框架DNN使用01
我先简单地介绍下我个人对于DNN的浅显理解吧. 我觉得对于刚接触的人来说首先理解DNN的原理,大框架是很重要的.它整个网站其实是没几个页面的,从源码上就可以看出, 一个Default页.一个Error ...
- scribe、chukwa、kafka、flume日志系统对比 -摘自网络
1. 背景介绍许多公司的平台每天会产生大量的日志(一般为流式数据,如,搜索引擎的pv,查询等),处理这些日志需要特定的日志系统,一般而言,这些系统需要具有以下特征:(1) 构建应用系统和分析系统的桥梁 ...