题目描述

Chiaki is interested in an infinite sequence a1, a2, a3, ..., which defined as follows:

Chiaki would like to know the sum of the first n terms of the sequence, i.e. . As this number may be very large, Chiaki is only interested in its remainder modulo (109 + 7).

输入描述:

There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 105), indicating the number of test cases. For each test case:
The first line contains an integer n (1 ≤ n ≤ 1018).

输出描述:

For each test case, output an integer denoting the answer.

示例1

输入

10
1
2
3
4
5
6
7
8
9
10

输出

0
1
2
2
4
4
6
7
8
11

题意:根据所给公式求绝对值之和

思路:官方题解

直接数位dp记忆化搜索。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
const int mod=1e9+7;
typedef long long LL;
LL dp[70][3][140];
int dig[70];
int judge (int i,int j)
{
    return i==j?1:-1;
}
LL dfs(int pos,int limit,int num,int front)
{
    if (pos<0) return abs(70-num);
    if (!limit&&dp[pos][front][num]!=-1) return dp[pos][front][num];
    int end=limit?dig[pos]:1;
    LL res=0;
    for (int i=0;i<=end;i++)
    {
        if (front==2&&i==1) res+=dfs(pos-1,limit&(i==end),num,i);
        else if (front==2&&i==0)    res+=dfs(pos-1,limit&(i==end),num,2);
        else res+=dfs(pos-1,limit&(i==end),num+judge(i,front),i);
        res%=mod;
    }
    if (!limit)
        dp[pos][front][num]=res;
    return res;
}
int main()
{
    memset(dp,-1,sizeof(dp));
    LL n;
    LL t;
    scanf("%lld",&t);
    while (t--)
    {
        scanf("%lld",&n);
        int len=0;
        while (n) dig[len++]=n&1, n>>=1;
        printf("%lld\n",dfs(len-1,1,70,2));
    }  
    return 0;
}

牛客网暑期ACM多校训练营(第四场)C(Chiaki Sequence Reloaded)相关推荐

  1. 牛客网暑期ACM多校训练营(第三场): C. Chiaki Sequence Reloaded(数位DP)

    题目描述 Chiaki is interested in an infinite sequence a1, a2, a3, ..., which defined as follows: Chiaki ...

  2. 牛客网暑期ACM多校训练营(第九场)

    牛客网暑期ACM多校训练营(第九场) A. Circulant Matrix 做法:看到下标 \(xor\) 这种情况就想 \(FWT\),可是半天没思路,于是放弃了..其实这个 \(n\) 疯狂暗示 ...

  3. 牛客网暑期ACM多校训练营(第一场)

    牛客网暑期ACM多校训练营(第一场) A. Monotonic Matrix 考虑0和1的分界线,1和2的分界线,发现问题可以转化为两条不互相穿过的路径的方案数(可重叠),题解的做法就是把一条路径斜着 ...

  4. 牛客网暑期ACM多校训练营(第二场): H. travel(树形线头DP)

    链接:https://ac.nowcoder.com/acm/contest/140/H 来源:牛客网 题目描述 White Cloud has a tree with n nodes.The roo ...

  5. 牛客网暑期ACM多校训练营(第三场): E. Sort String(KMP)

    链接:https://www.nowcoder.com/acm/contest/141/E 来源:牛客网 题目描述 Eddy likes to play with string which is a ...

  6. 牛客网暑期ACM多校训练营(第三场): C. Shuffle Cards(splay)

    链接:https://www.nowcoder.com/acm/contest/141/C 来源:牛客网 题目描述 Eddy likes to play cards game since there ...

  7. 牛客网暑期ACM多校训练营(第二场)A .run

    链接:https://www.nowcoder.com/acm/contest/140/A 来源:牛客网 题目描述 White Cloud is exercising in the playgroun ...

  8. 牛客网暑期ACM多校训练营(第一场) J (莫队算法)

    题目链接:https://www.nowcoder.com/acm/contest/139/J 题目大意:给一个序列,进行q次查询,问1~l和r~n中有多少个不同的数字 题目思路:之前只是听说过莫队算 ...

  9. 牛客网暑期ACM多校训练营(第二场)J farm (二维树状数组)

    题目链接: https://www.nowcoder.com/acm/contest/140/J 思路: 都写在代码注释里了,非常好懂.. for_each函数可以去看一下,遍历起vector数组比较 ...

  10. 牛客网 暑期ACM多校训练营(第一场)J.Different Integers-区间两侧不同数字的个数-离线树状数组 or 可持久化线段树(主席树)...

    J.Different Integers 题意就是给你l,r,问你在区间两侧的[1,l]和[r,n]中,不同数的个数. 两种思路: 1.将数组长度扩大两倍,for(int i=n+1;i<=2* ...

最新文章

  1. css实现元素居中的常见方法
  2. 你真的会用 regexp_replace 吗?
  3. Redis解决秒杀中的超卖问题
  4. mysql 自动 分库 备份_MySQL分库备份的方法
  5. apk反编译、smali修改、回编译笔记
  6. 通过MDaemon自带功能,实现同部门用同一账号对外收发邮件
  7. QCon全球软件开发大会:推动创新,实现技术落地
  8. GoogleCpp风格指南 8)格式 _part1
  9. 【基础教程】基于matlab生成Word+PPT报告【含Matlab源码 971期】
  10. 博客中GIF动画超简单制作
  11. Material Dialogs的使用
  12. Cadence Allegro如何复用设计参数?
  13. winpCap中compile和nocap函数
  14. Scala简介以及基本语法(一)
  15. java: 错误: 找不到符号
  16. 信必优新技术–QA 的新机遇
  17. 西工大机考《 催化剂工程》大作业网考
  18. 重复制造 成本收集器 月结 Period-end closing Product cost collector
  19. 51单片机——电脑通过串口控制LED
  20. vue+canvas实现手写签字画板

热门文章

  1. jTopo入门 简单实现拓扑图
  2. windows和android双系统平板,安卓+windows双系统平板的“得与失”
  3. CKA考试题梳理---加油,骚年 记得点赞收藏
  4. 华为机试练习(七)书籍叠放问题
  5. Mysql 建立外键出错1822 可能原因之一分析 - Failed to add the foreign key constraint. Missing index for constraint
  6. vue-cli: v-on事件绑定 的用法
  7. 1.结束进程的正确方式
  8. [Java]窗口的跳转
  9. myeclipse超详细安装教程+图文+安装包获取
  10. Word控件Spire.Doc更新至最新版v10.7,增强产品稳定性,修复老版本漏洞