九度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,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- this的三个要点
1.this的指向是什么? 指向对象 2.this可以书写在哪里? 可以写在全局,也可以写在函数里 三种写在函数里的方式: 2.1 this可变 function f() { this.name = ...
- IDEA 启用/禁用 Run Dashboard
一.启用 方式一: 创建/打开一个SpringBoot项目[或者点击Run --> Edit Configurations 添加 Spring Boot 类型的项目配置:或者如图在红框处添加配置 ...
- java.lang.IllegalAccessException: Class XX can not access a member of class XXX with modifiers "private static"
当前需求: 利用反射获取某一属性值运行结果:java.lang.IllegalAccessException: Class com.example.demo.test.Reflect can not ...
- ZOJ 3537 Cake (区间DP,三角形剖分)
题意: 给出平面直角坐标系上的n个点的坐标,表示一个多边形蛋糕,先判断是否是凸多边形,若否,输出"I can't cut.".若是,则对这个蛋糕进行3角形剖分,切n-3次变成n-2 ...
- Servlet和JSP之JSTL学习
JSTL JSTL就是JSP标准标签库(JavaServer Pages Standard Tag Library, JSTL)是一个定制标签库的集合,用来解决像遍历Map或集合.条件测试.XML处理 ...
- OpenGL Frustum参数设置
opengl中使用Frustum来设置透视投影,函数原型: frustum(float left, float right, float buttom, float top, float near, ...
- ssh复制remote
rsync rsync localdirectory username@10.211.55.4:/home/username/Downloads/localdirectory -r
- Objective-C分类 (category)和扩展(Extension) 的区别
http://blog.csdn.net/yhawaii/article/details/6992094 http://blog.163.com/wangy_0223/blog/static/4501 ...
- Python 解压序列、可迭代对象并赋值给多个变量
Python数据结构和类型 1.1 解压序列赋值给多个变量 现在有一个包含N个元素的元组或者是序列,怎样将它里面的值解压后同时赋值给N个变量? 解决思路:先通过简单的解压赋值给多个变量,前提是变量的数 ...
- Ubuntu 18.04 上使用 OpenJDK 安装并运行 Tomcat
在Linux上安装与卸载JDK和JRE,两种常用方法: 一.通过 apt-get 命令在线进行安装与卸载(会自动配置好环境变量) 二.通过下载并解压 .tar.gz 包进行手动安装与手动卸载(需要手动 ...