九度oj 题目1491:求1和2的个数
- 题目描述:
-
给定正整数N,函数F(N)表示小于等于N的自然数中1和2的个数之和,例如:1,2,3,4,5,6,7,8,9,10序列中1和2的个数之和为3,因此F(10)=3。输入N,求F(N)的值,1=<N<=10^100(10的100次方)若F(N)很大,则求F(N)mod20123的值。
- 输入:
-
输入包含多组测试数据,每组仅输入一个整数N。
- 输出:
-
对于每组测试数据,输出小于等于N的自然数中1和2的个数之和,且对20123取模。
- 样例输入:
-
10
11
- 样例输出:
-
3
5
- 提示:
-
建议用scanf ("%s")输入,而不建议用gets()!
这道题好难
开始用的思路简单,但必然超时
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#define inf 0x3f3f3f3f
#define MAX 102
using namespace std; char N[MAX];
int temp[MAX];
int end[MAX]; int inc(int wei) {
int ci = ;
for(int i = ; i < wei; i++) {
int sum = temp[i] + ci;
int ben = sum%;
ci = sum/;
temp[i] = ben;
}
if(ci == ) {
temp[wei] = ;
wei++;
return wei;
}
else {
return wei;
} } bool isEqual(int n) {
for(int i = ; i < n; i++) {
if(temp[i] != end[i]) {
return false;
}
}
return true;
} void show(int wei) {
for(int i = wei-;i >= ; i--) {
printf("%d",temp[i]);
}
puts("");
} void showE(int n) {
for(int i = n-;i >= ; i--) {
printf("%d",end[i]);
}
puts("");
} int main(int argc, char const *argv[])
{
while(scanf("%s",N) != EOF) {
int weiSum = strlen(N);
int wei = ;
for(int i = ; i < strlen(N); i++) {
temp[i] = ;
end[i] = ;
}
for(int i = ,j = strlen(N) - ; i < strlen(N); i++, j--) {
end[j] = N[i] - '';
}
//showE(weiSum);
temp[] = ;
int ans = ;
while(!isEqual(weiSum)) {
int ttt = ;
for(int i = ; i < wei; i++) {
if(temp[i] == || temp[i] == ) {
ttt++;
}
}
ans = (ans + ttt) % ;
wei = inc(wei);
//show(wei);
}
int ttt = ;
for(int i = ; i < weiSum; i++) {
if(end[i] == || end[i] == ) {
ttt++;
}
}
ans = (ans + ttt) % ;
printf("%d\n", ans);
}
return ;
}后一种思路是这样的,比如算123, 先求F(1),再求F(2),再求F(3)
先上代码
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#define inf 0x3f3f3f3f
#define MAX 102
using namespace std; char N[MAX]; int main(int argc, char const *argv[])
{
while(scanf("%s",N) != EOF) {
int result = ;
int count12 = ;
int num = ;
for(int i = ; i < strlen(N); i++) {
int temp = N[i] - '';
int q = ;
if(temp == ) {
q = ;
}
else if(temp == ) {
q = ;
}
else if(temp == ) {
q = ;
}
//个位 2 * num + q
//前面的位(前1) (result - count12) * (temp+1)
//本身 count12 * (temp+1) //for example 123
// 2 * 12 + 2 = 26
// F(11) = result - count12 F(11)*10
// 123 12有2位 ,后面0 1 2 3 2 * 4 即 count12*(temp+1)
result = * num + q + (result - count12) * + count12 * (temp+); if(N[i] == '' || N[i] == '') {
count12++;
}
num = num * + temp;
num = num % ;
result = result % ;
}
printf("%d\n", result);
}
return ;
}主要的思想是分位来统计1和2的个数,求出前n-1位的值,再求出总共n位的值
对于个位而言,前面0 - (num-1)共有num个数, 每10个数有2个1和2,所以共有 2*num个数
前面是num,后面有q个2
对于前面的位而言,F(N-1) = result - count12, 每一个有10个各位,共有 10 * F(N-1)个
对于num, num中有count12个1,2 后面那位是temp , 0-temp有temp+1个,共有 count12 * (temp+1)个
九度oj 题目1491:求1和2的个数的更多相关文章
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ题目1105:字符串的反码
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
- 九度oj题目1002:Grading
//不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...
- 九度OJ题目1003:A+B
while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- 前端常用的jquery代码
主要是个人在工作中常用到的一些代码,会慢慢添加: 1).enter键时可以触发某些事件,比如登陆事件: $('#loginform').bind('keypress',function(event){ ...
- 从零开始利用vue-cli搭建简单音乐网站(四)
上一篇文章中说到这一篇博客会实现音乐播放功能,只是令我意外的是,如果利用h5的audio标签,几行代码就实现了......先来看一下最终效果吧. 这里直接用了audio标签,样式没有怎么管,能获得音乐 ...
- 使用 Cosmos DB 创建和查询 NoSQL 表
本教程演示如何使用 Azure 门户创建 Azure Cosmos DB 帐户,然后使用 DocumentDB .NET API 创建具有分区键的文档数据库和集合.通过在创建集合时定义分区键,应用程序 ...
- 从零开发分布式数据库中间件 二、构建MyBatis的读写分离数据库中间件
在上一节 从零开发分布式数据库中间件 一.读写分离的数据库中间件 中,我们讲了如何通过ThreadLocal来指定每次访问的数据源,并通过jdbc的连接方式来切换数据源,那么这一节我们使用我们常用的数 ...
- C# 语言 类
++++String类+++++黑色小扳手 - 属性紫色立方体 - 方法 ***字符串.Length - 字符串长度,返回int类型 字符串.TrimStart() - 去掉前空格字符串.TrimEn ...
- Android(java)学习笔记143:Android中View动画之 XML实现 和 代码实现
1.Animation 动画类型 Android的animation由四种类型组成: XML中: alph 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动 ...
- Xcode4删除文件后missing file警告
1.运行终端,执行命令行进入missing file目录,然后运行 svn delete nameOfMissingFile 或 git rm nameOfMissingFile 2.删除隐藏的.sv ...
- JavaWeb项目实现图片验证码
一.什么是图片验证码? 可以参考下面这张图: 我们在一些网站注册的时候,经常需要填写以上图片的信息. 这种图片验证方式是我们最常见的形式,它可以有效的防范恶意攻击者采用恶意工具,调用“动态验证码短信获 ...
- Spring @Transactional at interface
java - Where should I put @Transactional annotation: at an interface definition or at an implementin ...
- CAD交互绘制块引用对象(网页版)
主要用到函数说明: _DMxDrawX::DrawBlockReference 绘制块引用对象.详细说明如下: 参数 说明 DOUBLE dPosX 插入点的X坐标 DOUBLE dPosY 插入点的 ...