题目大意:
  有一个函数f(n),满足3f(n)*f(2n+1)=f(2n)*(1+3f(n)),f(2n)<6f(n)。
  我们用g(t)表示f(i)%k=t的i的个数,其中1<=i<=n。
  问对于0<=x<k,所有的g(x)的异或和。

思路:
  将函数用递推式表示为:
  f(2n)=3f(n)
  f(2n+1)=f(2n)+1
  也就是说f(n)就是将n的二进制数串当作一个三进制数来算的值。
  接下来就是一个简单的数位DP。
  f[i][j]表示DP到第i位,前面那么多数所构成的三进制的值在模k意义下的值。

 #include<cstdio>
#include<cctype>
#include<cstring>
typedef long long int64;
inline int64 getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int64 x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int K=;
inline int log2(const float &x) {
return ((unsigned&)x>>&)-;
}
int64 f[][K];
inline int64 calc(const int64 &n,const int &k) {
int sum=;
const int len=log2(n);
memset(f[len&],,sizeof *f);
for(register int i=len;i>=;i--) {
memset(f[!(i&)],,sizeof *f);
const int cur=n>>i&;
sum=(sum*)%k;
for(register int j=;j<cur;j++) {
f[i&][(sum+j)%k]++;
}
sum=(sum+cur)%k;
for(register int j=;j<k;j++) {
f[!(i&)][j*%k]+=f[i&][j];
f[!(i&)][(j*+)%k]+=f[i&][j];
}
}
f[][]--;
f[][sum]++;
int64 ans=;
for(register int i=;i<k;i++) {
ans^=f[][i];
}
return ans;
}
int main() {
for(register int T=getint();T;T--) {
int64 n=getint();
int k=getint();
printf("%lld\n",calc(n,k));
}
return ;
}

[HihoCoder1259]A Math Problem的更多相关文章

  1. hdu 1757 A Simple Math Problem (乘法矩阵)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. HDU1757 A Simple Math Problem 矩阵快速幂

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. HDU 5615 Jam's math problem

    Jam's math problem Problem Description Jam has a math problem. He just learned factorization.He is t ...

  4. hdu----(5055)Bob and math problem(贪心)

    Bob and math problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. hdu------(1757)A Simple Math Problem(简单矩阵快速幂)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. BestCoder Round #70 Jam's math problem(hdu 5615)

    Problem Description Jam has a math problem. He just learned factorization. He is trying to factorize ...

  7. FZYZ-2071 A Simple Math Problem IX

    P2071 -- A Simple Math Problem IX 时间限制:1000MS      内存限制:262144KB 状态:Accepted      标签:    数学问题-博弈论    ...

  8. Jam's math problem(思维)

    Jam's math problem Submit Status Practice HDU 5615   Description Jam has a math problem. He just lea ...

  9. HDU 5055 Bob and math problem(结构体)

    主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5055 Problem Description Recently, Bob has been think ...

随机推荐

  1. Python与RPC -- (转)

    XML-RPC xmlrpc是使用http协议做为传输协议的rpc机制,使用xml文本的方式传输命令和数据. 一个rpc系统,必然包括2个部分: 1)rpc client,用来向rpc server调 ...

  2. Apache2.4+Tomcat7 集群

    Apache2.4+Tomcat7 集群: 1.下载并安装相对应的软件 apache下载地址:http://httpd.apache.org/ 这里使用apache2.4 tomcat下载地址:htt ...

  3. 关于RecylerView:1.在ScrollView的RecylerView滑动事件的处理。2.item之间的距离 小数取整

    1.在ScrollView的RecylerView滑动事件的处理. 在布局文件中在RecylerView外包裹一层相对布局 2.RecylerView item之间的距离 (1)编写SpaceItem ...

  4. 多线程伪共享FalseSharing

    1. 伪共享产生: 在SMP架构的系统中,每个CPU核心都有自己的cache,当多个线程在不同的核心上,并且某线程修改了在同一个cache line中的数据时,由于cache一致性原则,其他核心cac ...

  5. C基础 时间业务实战代码

    引言 业务代码中遇到这样需求, 1. 二者是同一天吗, 2. 时间戳和时间串来回转, 3. 其它扩展需求 等. C写代码同样需要处理这方面时间问题. 本文就是为了解决这个问题. 相比其它时间库, 这里 ...

  6. 初学者学习Javascript很吃力怎么办?到底该如何学习Js?

      Js给初学者的印象总是那么的“杂而乱”,相信很多初学者都在找轻松学习Js的途径.在这里给大家总结一些学习Js的经验,希望能给后来的学习者探索出一条“轻松学习Js之路”. Js给人那种感觉的原因多半 ...

  7. dubbo支持的远程调用方式

    dubbo RPC(二进制序列化 + tcp协议).http invoker(二进制序列化 + http协议,至少在开源版本没发现对文本序列化的支持).hessian(二进制序列化 + http协议) ...

  8. JDBC数据源连接池(3)---Tomcat集成DBCP

    此文续<JDBC数据源连接池(2)---C3P0>. Apache Tomcat作为一款JavaWeb服务器,内置了DBCP数据源连接池.在使用中,只要进行相应配置即可. 首先,确保Web ...

  9. python_day1学习笔记

    一.Python 2.7.x 和 3.x 版本的区别小结 print函数 1.python2 import platform print ‘Python’, platform.python_versi ...

  10. tk界面版股票下载

    from tkinter import * import urllib.request import re,os import threading from tkinter import filedi ...