题目链接:http://lightoj.com/volume_showproblem.php?problem=1132

题意:给出n和m。求sum(i^m)%2^32。(1<=i<=n) (1<=n<=10^15,0<=m<=50)。

思路:本题有两种方法:二分和矩阵。

(1)二分:设我们用DFS(n,m)来计算上面的式子。假如n为奇数,比如n=13,那么我们单独计算13^m,那么剩下的是n=12。前一半是DFS(6,m),后一半是7^m+8^m+……12^m。

进而n的规模就减小了一半。

(2)矩阵:这里我们假设m=4

设该矩阵为A ,B=[1,1,1,1,1],那么B*A^(n-1)的第一个元素就是n^4。

设该矩阵为C,D=[1,1,1,1,1,1],那么D*C^(n-1)的第一个元素就是1^4+2^4+……+n^4。

下面只给出二分的代码。

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <map>

#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define abs(x) ((x)>=0?(x):-(x))
#define i64 long long
#define u32 unsigned int
#define u64 unsigned long long
#define clr(x,y) memset(x,y,sizeof(x))
#define CLR(x) x.clear()
#define ph(x) push(x)
#define pb(x) push_back(x)
#define Len(x) x.length()
#define SZ(x) x.size()
#define PI acos(-1.0)
#define sqr(x) ((x)*(x))
#define MP(x,y) make_pair(x,y)

#define FOR0(i,x) for(i=0;i<x;i++)
#define FOR1(i,x) for(i=1;i<=x;i++)
#define FOR(i,a,b) for(i=a;i<=b;i++)

#define rush() int CC;for(scanf("%d",&CC);CC--;)
#define Rush(n)  while(scanf("%d",&n)!=-1)
using namespace std;

void RD(int &x){scanf("%d",&x);}
void RD(i64 &x){scanf("%lld",&x);}
void RD(u32 &x){scanf("%u",&x);}
void RD(double &x){scanf("%lf",&x);}
void RD(int &x,int &y){scanf("%d%d",&x,&y);}
void RD(i64 &x,i64 &y){scanf("%I64d%I64d",&x,&y);}
void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}
void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}
void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}
void RD(i64 &x,i64 &y,i64 &z){scanf("%I64d%I64d%I64d",&x,&y,&z);}
void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}
void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}
void RD(char &x){x=getchar();}
void RD(char *s){scanf("%s",s);}
void RD(string &s){cin>>s;}

void PR(int x) {printf("%d\n",x);}
void PR(int x,int y) {printf("%d %d\n",x,y);}
void PR(i64 x) {printf("%lld\n",x);}
void PR(u32 x) {printf("%u\n",x);}
void PR(double x) {printf("%.3lf\n",x);}
void PR(char x) {printf("%c\n",x);}
void PR(char *x) {printf("%s\n",x);}
void PR(string x) {cout<<x<<endl;}

const int mod=10056;
const i64 inf=((i64)1)<<60;
const double dinf=1e50;
const int INF=1000000000;
const int N=1000005;

const i64 M=((i64)1)<<32;

i64 n;
int m;
u32 C[55][55];

u32 Pow(i64 x,int y)
{
    u32 ans=1,temp=(u32)(x%M);
    while(y)
    {
        if(y&1) ans=ans*temp;
        temp=temp*temp;
        y>>=1;
    }
    return ans;
}

void init()
{
    int i,j;
    C[0][0]=1;
    for(i=1;i<=50;i++)
    {
        C[i][i]=C[i][0]=1;
        for(j=1;j<i;j++) C[i][j]=C[i-1][j-1]+C[i-1][j];
    }
}

map<pair<i64,int> ,u32> mp;

u32 DFS(i64 n,int m)
{
    if(m==0) return (u32)(n%M);
    if(n==1) return 1;
    if(mp.find(MP(n,m))!=mp.end()) return mp[MP(n,m)];

u32 ans=0;
    if(n&1) ans=Pow(n,m);
    u32 y=1,i;
    for(i=0;i<=m;i++)
    {
        ans+=C[m][m-i]*y*DFS(n/2,m-i);
        y=y*((u32)(n/2%M));
    }
    ans+=DFS(n/2,m);
    mp[MP(n,m)]=ans;
    return ans;
}

int main()
{
    init();
    int num=0;
    rush()
    {
        RD(n); RD(m);
        printf("Case %d: ",++num);
        PR(DFS(n,m));
    }
}

lightOJ 1132 Summing up Powers(矩阵 二分)的更多相关文章

  1. LightOJ - 1132 Summing up Powers 矩阵高速幂

    题目大意:求(1^K + 2^K + 3K + - + N^K) % 2^32 解题思路: 借用别人的图 能够先打表,求出Cnm,用杨辉三角能够高速得到 #include<cstdio> ...

  2. LightOJ 1132 Summing up Powers:矩阵快速幂 + 二项式定理

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1132 题意: 给定n.k,求(1K + 2K + 3K + ... + NK) % 2 ...

  3. HDU 2254 奥运(矩阵+二分等比求和)

    奥运 [题目链接]奥运 [题目类型]矩阵+二分等比求和 &题解: 首先离散化城市,之后就是矩阵快速幂了,但让求的是A^(t1)+A^(t1+1)+...+A^(t2),我先想的是打表,但时间真 ...

  4. UVA-10689 Yet another Number Sequence (矩阵二分幂模板)

    题目大意:已知递推公式和边缘值,求某项的最后m(0<m<5)位数字. 题目分析:矩阵二分幂的模板题. 代码如下: # include<iostream> # include&l ...

  5. 1142 - Summing up Powers (II)

    1142 - Summing up Powers (II)    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit ...

  6. Poj 3233 Matrix Power Series(矩阵二分快速幂)

    题目链接:http://poj.org/problem?id=3233 解题报告:输入一个边长为n的矩阵A,然后输入一个k,要你求A + A^2 + A^3 + A^4 + A^5.......A^k ...

  7. LightOj 1065 - Number Sequence (矩阵快速幂,简单)

    题目 和 LightOj 1096 - nth Term 差不多的题目和解法,这道相对更简单些,万幸,这道比赛时没把模版给抽风坏. #include<stdio.h> #include&l ...

  8. POJ 3233 Matrix Power Series (矩阵+二分+二分)

    题目地址:http://poj.org/problem?id=3233 题意:给你一个矩阵A,让你求A+A^2+……+A^k模p的矩阵值 题解:我们知道求A^n我们可以用二分-矩阵快速幂来求,而 当k ...

  9. Wannafly模拟赛 A.矩阵(二分答案+hash)

    矩阵 时间限制:1秒 空间限制:131072K 题目描述 给出一个n * m的矩阵.让你从中发现一个最大的正方形.使得这样子的正方形在矩阵中出现了至少两次.输出最大正方形的边长. 输入描述: 第一行两 ...

随机推荐

  1. python之range(), xrange()

    可以这样理解: range()立即执行,返回结果 xrange()延迟执行,需要时再返回结果.

  2. iOS多线程编程Part 3/3 - GCD

    前两部分介绍了NSThread.NSRunLoop和NSOperation,本文聊聊2011年WWDC时推出的神器GCD.GCD: Grand Central Dispatch,是一组用于实现并发编程 ...

  3. quartus ii 中文注释乱码解决办法

    转载自:http://bbs.ednchina.com/BLOG_ARTICLE_3027549.HTM 有些时候我们用Quartus ii 打开不同版本创建的工程文件时,往往会出现下列提示 点yes ...

  4. (转) java 复制文件,不使用输出流复制,高效率,文件通道的方式复制文件

    public static void fileChannelCopy(File s, File t) { FileInputStream fi = null; FileOutputStream fo ...

  5. Elasticsearch 5.0

    Elasticsearch 5.0 使用ES的基本都会使用过head,但是版本升级到5.0后,head插件就不好使了.下面就看看如何在5.0中启动Head插件吧! 官方粗略教程 Running wit ...

  6. SVN的405错误

    错误1: 如果你没有阅读以下文字,活该你倒霉(这段文字是在googlecode添加新项目时生成的): Command-line access If you plan to make changes, ...

  7. 《C++Primer》复习——with C++11 [2]

    1.数组引用形参,C++允许将变量定义成数组的引用,给予同样的道理,形参也可以是数组的引用,此时引用形参绑定到对应的实参上,也就是绑定到数组上 ]) { for (auto elem : arr) c ...

  8. 设置google搜索打开链接时在新标签页显示

    百度的搜索结果,打开链接都会在新的页面打开,但是google却直接在本页面打开,有时候我们打开的不一定是自己想要结果,又习惯性的把当前页面给关掉了......这只是习惯问题,可能国人有这个习惯.怎么设 ...

  9. javaScript基础之闭包

    不管是Jquery还是EXTJS,现代的js框架中大量应用了js的一些特性,比如:匿名函数,闭包等等,导致源代码非常难以阅读. 不过要想真正的使用好前台的UI技术,还是需要去深入的理解这些概念.   ...

  10. "Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b7

    1.错误背景 系统安装了.net framework4.0.4.5,项目先使用VS2013(4.5)开发,后来又重新用VS2010开发(4.0),运行时出现这个错误 2.错误原因 In .Net 4. ...