zoukankan      html  css  js  c++  java
  • 【计数】Simple Addition Expression

    【来源】

    2008年哈尔滨区域赛

    【题目链接】:

    http://acm.hdu.edu.cn/showproblem.php?pid=2451

    【参考博客】:

    HDU 2451 Simple Addition Expression

    【题意】:

    题意是要判断前n位数字(不包括n),有多少个数字 i 跟前面两个 i+1 , i+2 ,相加时不进位 。

    符合要求的数字就是个位 0 ~ 2 ,其余位 0 ~ 3。

    用一个dfs就可以搜出来了。

    对于当前位是 x 的话 , 若果 x > 3 , 可以直接得出可以构成  [ 4^(位数-1) * 3 ] 个数

    若果 x <= 3 的话 , 就可以构成 [  (x-1)*4^(位数-1)*3 + 后面的位能组成数字的数目(递归求) ]。

     【注意】

    我第一次交的时候忽略了一种情况,就是其他位置(除个位外)为3的情况。

    真的忽略了,一直以为都是3*3*3.....,其实是4*4*4*.....*3

    别人都是用回溯什么写的,

    我自己用for循环写出来了。

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<iostream>
     4 using namespace std;
     5 typedef long long ll;
     6 const ll N = 1e10;
     7 const ll M = 3145728;
     8 int main()
     9 {
    10     ll n;
    11     while(~scanf("%lld",&n)) {
    12         ll ans = 0, tmp = 1;
    13 
    14         if( n <= 9 ){
    15             printf("%lld
    ",min(3ll,n));
    16             continue ;
    17         }
    18         n ++ ;
    19         for (ll i = N, j = M; i >= 1; i /= 10, j /= 4) {
    20             if (n / i == 0) continue;
    21             if (n / i >= 4) {
    22                 //printf("# 1 %lld , %lld 
     ",i,j);
    23                 ans += tmp * j;
    24                 break;
    25             }
    26             if (n / i <= 3) {
    27                 ans += (n / i) * j/4 ;
    28                 //printf("# 2 %lld , %lld 
     ",i,j);
    29             }
    30 
    31             n = n % i ;
    32         }
    33         printf("%lld
    ", ans);
    34     }
    35     return 0;
    36 }
    View Code
  • 相关阅读:
    vue-webpack介绍
    vue-ES6模块化的导入和导出
    vue-前端模块化
    vue-插槽作用域的使用
    vue-具名插槽的使用
    vue-插槽的基本使用
    vue-父组件获取子组件对象实例
    IO(六)
    IO(五)
    关于overflow:hidden
  • 原文地址:https://www.cnblogs.com/Osea/p/11314140.html
Copyright © 2011-2022 走看看