lightOJ 1132 Summing up Powers(矩阵 二分)
题目链接: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(矩阵 二分)的更多相关文章
- LightOJ - 1132 Summing up Powers 矩阵高速幂
题目大意:求(1^K + 2^K + 3K + - + N^K) % 2^32 解题思路: 借用别人的图 能够先打表,求出Cnm,用杨辉三角能够高速得到 #include<cstdio> ...
- LightOJ 1132 Summing up Powers:矩阵快速幂 + 二项式定理
题目链接:http://lightoj.com/volume_showproblem.php?problem=1132 题意: 给定n.k,求(1K + 2K + 3K + ... + NK) % 2 ...
- HDU 2254 奥运(矩阵+二分等比求和)
奥运 [题目链接]奥运 [题目类型]矩阵+二分等比求和 &题解: 首先离散化城市,之后就是矩阵快速幂了,但让求的是A^(t1)+A^(t1+1)+...+A^(t2),我先想的是打表,但时间真 ...
- UVA-10689 Yet another Number Sequence (矩阵二分幂模板)
题目大意:已知递推公式和边缘值,求某项的最后m(0<m<5)位数字. 题目分析:矩阵二分幂的模板题. 代码如下: # include<iostream> # include&l ...
- 1142 - Summing up Powers (II)
1142 - Summing up Powers (II) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit ...
- 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 ...
- LightOj 1065 - Number Sequence (矩阵快速幂,简单)
题目 和 LightOj 1096 - nth Term 差不多的题目和解法,这道相对更简单些,万幸,这道比赛时没把模版给抽风坏. #include<stdio.h> #include&l ...
- POJ 3233 Matrix Power Series (矩阵+二分+二分)
题目地址:http://poj.org/problem?id=3233 题意:给你一个矩阵A,让你求A+A^2+……+A^k模p的矩阵值 题解:我们知道求A^n我们可以用二分-矩阵快速幂来求,而 当k ...
- Wannafly模拟赛 A.矩阵(二分答案+hash)
矩阵 时间限制:1秒 空间限制:131072K 题目描述 给出一个n * m的矩阵.让你从中发现一个最大的正方形.使得这样子的正方形在矩阵中出现了至少两次.输出最大正方形的边长. 输入描述: 第一行两 ...
随机推荐
- ASP.NET操作WMI
WMI Functions from ASP.NET Introduction This article demonstrates how to use WMI in ASP.NET to cre ...
- cocos2dx中的背景图层CCLayerColor和渐变图层CCLayerGradient
1.CCLayerColor是专门用来处理背景颜色的图层,它继承自CCLayer,可以用来设置图层的背景颜色,因为CCLayer默认是透明色的,即无颜色的 2.CCLayerGradient是用来显示 ...
- SQL Server2008附加数据库之后显示为只读
SQL Server2008附加数据库之后显示为只读时解决方法 啰嗦的话就不多说了,直入主题吧! 方案一: 碰到这中情况一般是使用的sa账户登录的,只要改为Windows身份验证,再附加数据库即可搞定 ...
- DB天气app冲刺二阶段第九天
今天是第九天了 不管怎么样也要收尾了赶紧,毕竟不可能做到尽善尽美了,时间不够了所以要把该砍掉的砍点,然后应对下周的大二同学的面试.尽量做好界面的美化工作这是最基本的了.毕竟我一直崇尚的就是UI设计了. ...
- SQL Server数据库事务日志序列号(LSN)介绍
原文:http://blog.csdn.net/tjvictor/article/details/5251463 日志序列编号(LSN)是事务日志里面每条记录的编号. 当你执行一次备份时,一些 ...
- ASP.Net MVC4 登录
一月之前,工作室师兄给我们介绍博客,然后试着去申请了一个,一直不晓得更新什么,直到今天,才有了一点小想法.最近在做一个小网站,然后就在登录那一块犯愁了,止步不前. 以前对登录一直没有什么概念,以为登录 ...
- 多线程学习之AsyncOperation实现线程间交互
1.首先我们要实现如下图的效果: a.主线程A运行方法段1时创建子线程B b.然后子线 ...
- xubuntu install nodejs
1.安装依赖sudo apt-get install g++ curl libssl-dev apache2-utils git-core 2.去官网获取最新版本 sudo wget http://n ...
- java 连接池的简单实现
最近一个项目中需要自己写个连接池, 写了一个下午,挺辛苦的,但不知道会不会出问题, 所以,贴到博客上,欢迎各路大神指点 1. 配置信息: /** * */ package cn.mjorcen.db. ...
- 如何在Eclipse中配置Tomcat服务器
之前使用MyEclipse来开发Web应用,可以在MyEclipse中配置服务器,配置完后,直接运行服务器即可,很方便. 最近切换到Eclipse开发环境,发现使用Tomcat的方式不太一样,因此在此 ...