九度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,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- nodejs中的异步回调机制
1.再次clear Timer定时器的作用 setTimeOut绝非是传统意义上的“sleep”功能,它做不到让主线程“熄火”指定时间,它是用来指定:某个回调在固定时间后插入执行栈!(实际执行时间略长 ...
- copyin函数
详见:http://pic.dhe.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.kerneltechref%2Fdoc%2Fk ...
- eclipse的hadoop插件对集群操作提示org.apache.hadoop.security.AccessControlException:Permission denied
eclipse的hadoop插件对集群操作提示org.apache.hadoop.security.AccessControlException:Permission denied: user = z ...
- Asp.net Mvc 表单验证(气泡提示)
将ASP.NET MVC或ASP.NET Core MVC的表单验证改成气泡提示: //新建一个js文件(如:jquery.validate.Bubble.js),在所有要验证的页面引用 (funct ...
- asp.net core mvc 异步表单(Ajax.BeginForm)
.net core中已经没有beginform扩展函数了. 通过Bower引入jquery-ajax-unobtrusive: <script src="~/lib/jquery-aj ...
- iOS上架问题解决
dns问题 http://iphone.91.com/tutorial/syjc/140509/21686339.html 网络问题 手机4g开wifi,上传提交多次 时间问题 东八区下午6点上架成功 ...
- Luogu P5352 Terrible Homework
神仙@TheLostWeak出的题,因为他最近没时间所以我先写一下sol(其实我也没什么时间) 作为一道简单的数据结构题想必大家都能看出必须用LCT维护信息吧 一个朴素的想法就是直接维护四种操作的值, ...
- node mocha mochawesome报安装不成功
1.进行cnpm安装: npm install cnpm -g --registry=https://registry.npm.taobao.org 2.查看cnpm版本 cnpm -v 3.安装mo ...
- rf统计条数
js模式 直接引用关键字模式
- DP玄学优化——斜率优化
--以此博客来悼念我在\(QBXT\)懵逼的时光 \(rqy\; tql\) (日常%\(rqy\)) 概念及用途 斜率优化是\(DP\)的一种较为常用的优化(据说在高中课本里稍有提及),它可以用于优 ...