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. JS中的DOM操作和事件

    [DOM树节点] DOM节点分为三大类: 元素节点. 属性节点. 文本节点: 文本节点.属性节点属于元素节点的子节点.操作时,均需要先取到元素节点,再操作子节点:可以使用getElement系列方法, ...

  2. SQL Server锁类型

    SQL Server锁类型(SQL)收藏 1. HOLDLOCK: 在该表上保持共享锁,直到整个事务结束,而不是在语句执行完立即释放所添加的锁. 2. NOLOCK:不添加共享锁和排它锁,当这个选项生 ...

  3. Java 反射之动态代理

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt205 利用Java反射机制你可以在运行期动态的创建接口的实现.java.la ...

  4. WCF(一)基础整理

    学习WCF之前,了解下WCF和WebService的区别. WCF和WebService区别 Web Service严格来说是行业标准,也就是Web Service 规范,它使用XML扩展标记语言来表 ...

  5. c# DateTime 类

    获得当前系统时间: DateTime dt = DateTime.Now;Environment.TickCount可以得到"系统启动到现在"的毫秒值DateTime now = ...

  6. ★RFC标准库_目录链接

    RFC(Request For Comments)是一个国际标准化的数据库,记录了从计算机到互联网的海量标准协议.它是一个免费公开的IT标准文件分享平台,其内容也在不断增长,与时俱进.它与ISO等组织 ...

  7. 即时作图新工具—ProcessOn【推荐】

    www.processon.com 推荐这个在线作图网站:免费登录,云端存储,面向对象,最重要的是提供了丰富模板! 在线软件的人气会越来越高,这是趋势啊~

  8. 个人作业3——个人总结(Alpha阶段)

    个人总结 在Alpha冲刺阶段中,我们团队基本完成了项目的大致基础框架,还有很多不足需要更多的时间来让我们做得更好. 对我个人而言,Alpha冲刺阶段是一个强度很大的阶段,每天都在吸收新的知识,团队也 ...

  9. Java多线程之Executor、ExecutorService、Executors、Callable、Future与FutureTask

    1. 引子 初学Java多线程,常使用Thread与Runnable创建.启动线程.如下例: Thread t1 = new Thread(new Runnable() { @Override pub ...

  10. Swing-BoxLayout用法-入门

    注:本文内容源于http://www.java3z.com/cwbwebhome/article/article20/200016.html?id=4797:细节内容根据笔者理解有修改. BoxLay ...