Power Sum

Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
SubmitStatus

Problem Description

给出n,m,p,求 (1^m + 2^m + 3^m + 4^m + ... + n^m) % p

Input

第一行一个数T( <= 10),表示数据总数

然后每行给出3个数n,m,p(1 <= n <= m <= 10^18, 1 <= p <= 10^6, p是质数

Output

每组数据输出你求得的结果

Sample Input

2
1 1 11
3 2 11

Sample Output

1
3

Hint

i^m即求 i * i * i * i * i... * i(m个i),比如2^3即2 * 2 * 2 
 
 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define ll long long
int fac[],fn,pri[],pn=;
void init()
{
int i,j;
for(i=;i<;i++)
{
if(!pri[i])
{
pri[pn++]=i;
j=i*i;
while(j<)
{
pri[j]=;
j+=i;
}
}
}
}
void findfac(int x)
{
fn=;
int i,j;
for(i=;x>=pri[i]&&i<pn;i++)
{
if(x%pri[i]==)
{
fac[fn++]=pri[i];
while(x%pri[i]==)x/=pri[i];
}
}
if(x!=)fac[fn++]=x;
}
ll power(ll x,ll y,ll mod)
{
ll ans=;
while(y)
{
if(y&)
{
ans*=x;
ans%=mod;
}
x*=x;
x%=mod;
y>>=;
}
return ans;
}
bool check(ll x,ll y)
{
ll z=y-;
for(ll i=;i<fn;i++)
if(power(x,z/fac[i],y)==)return ;
return ;
}
int findroot(int x)
{
if(x==)return ;
findfac(x-);
for(ll i=;;i++)
if(check(i,x))return i;
}
int poww[],repow[];
int dp[];
int main()
{
init();
int t,root,i,j;
ll n,m,p;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld%lld",&n,&m,&p);
root=findroot(p);
m%=p-;
int temp=;
for(i=;i<p;i++)
{
poww[i]=temp;
repow[temp]=i;
temp=temp*root%p;
}
dp[]=;
for(i=;i<p;i++)
{
dp[i]=(dp[i-]+poww[(repow[i]*m)%(p-)])%p;
}
printf("%d\n",dp[n%p]);
}
}

Power Sum 竟然用原根来求的更多相关文章

  1. ACDream - Power Sum

    先上题目: Power Sum Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) S ...

  2. FFT模板 生成函数 原根 多项式求逆 多项式开根

    FFT #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> ...

  3. [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  4. LightOj 1278 - Sum of Consecutive Integers(求奇因子的个数)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1278 题意:给你一个数n(n<=10^14),然后问n能用几个连续的数表示; 例 ...

  5. HDU 1024 Max Sum Plus Plus【动态规划求最大M子段和详解 】

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  6. [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和

    Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...

  7. [LeetCode] 129. Sum Root to Leaf Numbers 求根到叶节点数字之和

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  8. Max Sum Plus Plus---hdu1024(动态规划求M段的最大和)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意就是有n个数分成m段,求最大的和: dp[i][j]表示把 j 个数分成 i 段,选择第 j ...

  9. 129 Sum Root to Leaf Numbers 求根叶数字总和

    给定一个只包含 0-9 数字的二叉树,每个根到叶的路径可以代表一个数字.例如,从根到叶路径 1->2->3则代表数字 123.查找所有根到叶数字的总和.例如,    1   / \  2  ...

随机推荐

  1. css控制竖直文字显示

    假如有一天,你把水平文字看腻味了...... 我建议你不妨试试垂直文字显示,就像这样: 我 是 竖 直 显 示 的 : 哈哈! 言归正传,怎么把一段话,垂直显示呢? 方法1: //把下面代码另存为ht ...

  2. jmeter系列------参数关联

    接口请求中的一个变量需要用上一个接口道服务器返回响应的动态值(上个请求). 遇到这样的情况,我们就需要用到关联 例如用户A发表了一个一条微信朋友圈,用户B想对这条朋友圈进行评论,就需要先获取到这个朋友 ...

  3. php工具方法

    备忘常用方法 1.寻找子栏目(权限\菜单列表...) function getSon($list,$pid){ $arr=''; foreach ($list as $k=>$v){ if($v ...

  4. NSA武器库知识整理

    美国国家安全局(NSA)旗下的"方程式黑客组织"(shadow brokers)使用的部分网络武器被公开,其中包括可以远程攻破全球约70%Windows机器的漏洞利用工具. 其中, ...

  5. diff.js 列表对比算法 源码分析

    diff.js列表对比算法 源码分析 npm上的代码可以查看 (https://www.npmjs.com/package/list-diff2) 源码如下: /** * * @param {Arra ...

  6. 改变input光标颜色与输入字体颜色不同

    设置input css: color #ffd600text-shadow 0px 0px 0px #bababa -webkit-text-fill-color initial input, tex ...

  7. linux为用户配置java环境变量

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt226 一. 解压安装jdk 在shell终端下进入jdk-6u14-linu ...

  8. c# 读取excel数据的两种方法(转)

    转载自:http://developer.51cto.com/art/201302/380622.htm, 方法一:OleDb: 用这种方法读取Excel速度还是非常的快的,但这种方式读取数据的时候不 ...

  9. 转:【Java集合源码剖析】Vector源码剖析

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/35793865   Vector简介 Vector也是基于数组实现的,是一个动态数组,其容量 ...

  10. java1.8新特性

    转自:http://www.oschina.NET/translate/everything-about-Java-8 建议去看原文,此处转载只是为了记录. 这篇文章是对Java8中即将到来的改进做一 ...