Counting

The Problem

Gustavo knows how to count, but he is now learning how write numbers. As he is a very good student, he already learned 1, 2, 3 and 4. But he didn't realize yet that 4 is different than 1, so he thinks that 4 is another way to write 1. Besides that, he is
having fun with a little game he created himself: he make numbers (with those four digits) and sum their values. For instance:

132 = 1 + 3 + 2 = 6
112314 = 1 + 1 + 2 + 3 + 1 + 1 = 9 (remember that Gustavo thinks that 4 = 1)

After making a lot of numbers in this way, Gustavo now wants to know how much numbers he can create such that their sum is a number n. For instance, for n = 2 he noticed that he can make 5 numbers: 11, 14, 41, 44 and 2 (he knows how to count them up, but he
doesn't know how to write five). However, he can't figure it out for n greater than 2. So, he asked you to help him.

The Input

Input will consist on an arbitrary number of sets. Each set will consist on an integer n such that 1 <= n <= 1000. You must read until you reach the end of file.

The Output

For each number read, you must output another number (on a line alone) stating how much numbers Gustavo can make such that the sum of their digits is equal to the given number.

Sample Input

2
3

Sample Output

5
13

题意:Gustavo数数时总是把1和4搞混,他觉得4仅仅是1的第二种写法。给出一个整数n,Gustavo想知道有多少个数的数字之和恰好为n。比如,当n=2时,有5个数:11、14、41、44、2。

分析:如果 F(n) 表示使用 1。2,3,4 构建的和为 n 的序列总数,则这些序列中,以 1 为開始的序列种数为 F(n - 1)。以2为開始的为 F(n - 2)。以3開始的序列总数为 F(n - 3)、以4開始的序列总数为  F(n - 4),因为 Gustavo 把 4 当作 1,则有 F(n - 4) = F(n - 1),

 故 F(n) = F(n - 1) + F(n - 2) + F(n - 3) + F(n - 4) = 2 * F(n - 1) + F(n - 2) + F(n - 3)。

边界条件: F(1) = 2, F(2) = 5。 F(3) = 13。
#include<string>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std; vector<string> v; string add(string a, string b)
{
string s;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int i = 0;
int m, k = 0;
while(a[i] && b[i])
{
m = a[i] - '0' + b[i] - '0' + k;
k = m / 10;
s += (m % 10 + '0');
i++;
}
if(i == a.size())
{
while(i != b.size())
{
m = k + b[i] - '0';
k = m / 10;
s += m % 10 + '0';
i++;
}
if(k) s += k + '0';
}
else if(i == b.size())
{
while(i != a.size())
{
m = k + a[i] - '0';
k = m / 10;
s += m % 10 + '0';
i++;
}
if(k) s += k + '0';
}
reverse(s.begin(), s.end());
return s;
} void solve()
{
v.push_back("0");
v.push_back("2");
v.push_back("5");
v.push_back("13");
string s;
for(int i = 4; ; i++)
{
s = add(v[i-1], v[i-1]);
s = add(v[i-2], s);
s = add(v[i-3], s);
v.push_back(s);
if(v[i].size() > 1001) break;
}
} int main()
{
solve();
int n;
int Size = v.size();
while(cin >> n)
{
cout << v[n] << endl;
}
return 0;
}


版权声明:本文博主原创文章,博客,未经同意不得转载。

UVA 10198 Counting的更多相关文章

  1. uva 1436 - Counting heaps(算)

    题目链接:uva 1436 - Counting heaps 题目大意:给出一个树的形状,如今为这棵树标号,保证根节点的标号值比子节点的标号值大,问有多少种标号树. 解题思路:和村名排队的思路是一仅仅 ...

  2. UVA 12075 - Counting Triangles(容斥原理计数)

    题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对 ...

  3. UVA - 10574 Counting Rectangles

    Description Problem H Counting Rectangles Input: Standard Input Output:Standard Output Time Limit: 3 ...

  4. UVA 10574 - Counting Rectangles(枚举+计数)

    10574 - Counting Rectangles 题目链接 题意:给定一些点,求可以成几个边平行于坐标轴的矩形 思路:先把点按x排序,再按y排序.然后用O(n^2)的方法找出每条垂直x轴的边,保 ...

  5. UVA 10574 - Counting Rectangles 计数

    Given n points on the XY plane, count how many regular rectangles are formed. A rectangle is regular ...

  6. UVA 12075 Counting Triangles

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. UVA 11174 Stand in a Line,UVA 1436 Counting heaps —— (组合数的好题)

    这两个题的模型是有n个人,有若干的关系表示谁是谁的父亲,让他们进行排队,且父亲必须排在儿子前面(不一定相邻).求排列数. 我们假设s[i]是i这个节点,他们一家子的总个数(或者换句话说,等于他的子孙数 ...

  8. UVA 1393 Highways,UVA 12075 Counting Triangles —— (组合数,dp)

    先看第一题,有n*m个点,求在这些点中,有多少条直线,经过了至少两点,且不是水平的也不是竖直的. 分析:由于对称性,我们只要求一个方向的线即可.该题分成两个过程,第一个过程是求出n*m的矩形中,dp[ ...

  9. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

随机推荐

  1. Solr的安装

    1.   JDK要求 Solr 4.10 要求JDK版本必须是1.7或更高. 下载地址: http://www.apache.org/dyn/closer.cgi/lucene/solr/ 下载得到z ...

  2. 解决SQL Server 占用80端口

    停用掉下面的服务就可以了:

  3. C# 前台线程与后台线程区别

    using System; using System.Drawing; using System.Windows.Forms; using System.Threading; namespace Wi ...

  4. angularjs中ng-switch的用法

    <!DOCTYPE html> <html lang="zh-CN" ng-app="app" ng-controller="ctr ...

  5. h5connect.js 腾讯云视频点播使用指南

    http://video.qcloud.com/download/docs/QVOD_Player_Web_SDK_Developer_Guide.pdf 腾讯云视频点播服务 Web播放器SDK开发指 ...

  6. 当 IDENTITY_INSERT 设置为 OFF 时,不能为表中的标识列插入显式值。

    出现以上错误是应为在执行insert语句时,将自动增加的字段加入,导致报错. 解决办法:把自增列的字段从插入语句中删除

  7. While installing plugin in eclipse luna, “Unable to acquire PluginConverter service” and “No repository found” errors appear in logs

    http://stackoverflow.com/questions/18767831/while-installing-plugin-in-eclipse-luna-unable-to-acquir ...

  8. LINUX小技巧,如何在指定目录下搜索到含特定关键字的文件。

    先找出文件,然后将文件作为输入,找具体关键字 find /etc -name "*" | xargs grep "Hello"

  9. 一定要学会OutputDebugString,方便源码级调试

    省得到处自己print,麻烦的要死...

  10. 基于异步的MVC webAPI控制器

    MVC – Task-based Asynchronous Pattern (TAP) – Async Controller and SessionLess Controller Leave a re ...