九度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,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- Visual Studio使用技巧学习
F7: 代码窗口 Shift+F7: 对象窗口 F4: 属性窗口 闪电图标: 对象的事件 F5: 编译及运行 Ctrl+F5: 编译及运行(不调试) svm+两次Tab: s ...
- SQL Server 2008 转换为 SQL 2005 数据库 脚本生成
Tips: 本文讨论如何把数据库从SQL Server 2008版本降低到2005,因为在本地开发是以SQL Server 2008 Express Edition版本进行的,而主机提供商现在提供的M ...
- 字符串、String等转换
(1) String 转换为字符串 例:String s = "abcde";char[] a = s.toCharArray(); (2) 字符串转换为Stringchar[] ...
- 基于Vmware player的Windows 10 IoT core + RaspberryPi2安装部署
本文记录了基于Vmware Player安装Windows10和VS2015开发平台的过程,以及如何在RaspberryPi2.0上启动Windows10 IoT core系统,并通过一个简单的hel ...
- SpringAOP 设计原理
1. 设计原理 引入了,代理模式. java 程序执行流: 如果从虚拟机的角度看,整个程序的过程就是方法的调用,我们按照方法的执行顺序,将方法调用成一串. 在方法之间有着Join Point 连接点 ...
- Cayley凯莱定理——一一对应
定理 过$n$个有标志顶点的树的数目等于$n^{n-2}$. 此定理说明用$n-1$条边将$n$个已知的顶点连接起来的连通图的个数是$n^{n-1}$.也可以这样理解,将n个城市连接起来的树状网络有$ ...
- vue 中 $set 的使用
在我们使用vue进行开发的过程中,可能会遇到一种情况:当生成vue实例后,当再次给数据赋值时,有时候并不会自动更新到视图上去: <!DOCTYPE html> <html> & ...
- 用cssText批量修改样式
一般情况下我们用js设置元素对象的样式会使用这样的形式: var element= document.getElementById(“id”);element.style.width=”20px”;e ...
- Linux系统分区 进程管理 软件包安装
对于一块新的磁盘来说,系统能够使用需要有分区.格式化文件系统.挂载等主要操作,下面通过命令的方式对一块磁盘进行操作. 一. Linux系统分区 1.1 在虚拟机开机前选择虚拟机配置,添加一个新的SCS ...
- Python基础篇 -- 部分练习题
实现一个整数加法计算器(两个数相加): 如:content = input("请输入内容:") 用户输入:5+9或5+ 9或5 + 9(含空白),然后进行分割转换最终进行整数的计算 ...