九度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,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- cf580E. Kefa and Watch(线段树维护字符串hash)
题意 $n$个数的序列,$m + k$种操作 1.$l , r, k$把$l - r$赋值为$k$ 2.$l, r, d$询问$l - r$是否有长度为$d$的循环节 Sol 首先有个神仙结论:若询问 ...
- css水平垂直居中的几个方法和技巧/居中之美
水平居中设置-行内元素 我们在实际工作中常会遇到需要设置水平居中场景,今天我们就来看看怎么设置水平居中的. 如果被设置元素为文本.图片等行内元素时,水平居中是通过给父元素设置 text-ali ...
- Struts2标签<s:checkboxlist>回显问题
Struts2 checkboxlist回显问题中,说明两种方式,第一种方式很普遍,第二种则是个人根据现有资源加上尝试得来的成果,第二种主要是为个人笔记(其中相关知识点不一一介绍). 一.普通方法: ...
- codeforces Gym 100338C Important Roads (重建最短路图)
正反两次最短路用于判断边是不是最短路上的边,把最短路径上的边取出来建图.然后求割边.注意重边,和卡spfa. 正权,好好的dijkstra不用,用什么spfa? #include<bits/st ...
- 校内选拔I题题解 构造题 Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) ——D
http://codeforces.com/contest/574/problem/D Bear and Blocks time limit per test 1 second memory limi ...
- Ace 在Vue中使用方法
var Vue = require('vue/dist/vue.common.js'); document.querySelector('body').append(document.createEl ...
- git 添加 ,密匙
转载此处 https://blog.csdn.net/xiayiye5/article/details/79652296
- input提示文字;placeholder字体修改
在很多网站上我们都看到input输入框显示提示文字,让我们一起来看看如果在input输入框中显示提示文字.我们只需要在<input>标签里添加:placeholder="提示文字 ...
- faster rcnn需要理解的地方
http://blog.csdn.net/terrenceyuu/article/details/76228317 https://www.cnblogs.com/houkai/p/6824455.h ...
- python小括号( )与中括号 [ ]
在python中小括号()表示的是tuple元组数据类型,元组是一种不可变序列. >>> a = (1,2,3) >>> a (1, 2, 3) >>& ...