Power Sum 竟然用原根来求
Power Sum
Problem Description
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
#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 竟然用原根来求的更多相关文章
- ACDream - Power Sum
先上题目: Power Sum Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) S ...
- FFT模板 生成函数 原根 多项式求逆 多项式开根
FFT #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> ...
- [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 ...
- LightOj 1278 - Sum of Consecutive Integers(求奇因子的个数)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1278 题意:给你一个数n(n<=10^14),然后问n能用几个连续的数表示; 例 ...
- 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 ...
- [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...
- [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 ...
- Max Sum Plus Plus---hdu1024(动态规划求M段的最大和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意就是有n个数分成m段,求最大的和: dp[i][j]表示把 j 个数分成 i 段,选择第 j ...
- 129 Sum Root to Leaf Numbers 求根叶数字总和
给定一个只包含 0-9 数字的二叉树,每个根到叶的路径可以代表一个数字.例如,从根到叶路径 1->2->3则代表数字 123.查找所有根到叶数字的总和.例如, 1 / \ 2 ...
随机推荐
- html:table属性cellpadding
cellpadding:单元格边距(空白区域) colspan:可以横跨的列数(td/th都算一列) 详细:http://www.dreamdu.com/xhtml/attribute_cellpad ...
- VS2013禁用Browser Link
禁用原因 VS2013新增的Browser Link功能虽然“强大”,但我并不需要. 但默认是开启的,会在页面中自动添加如下的代码,查看AJAX时造成很大的干扰. <!-- Visual Stu ...
- fetch的使用说明
1.RN官方文档中,可使用XMLHttpRequest var request = new XMLHttpRequest(); request.onreadystatechange = (e) =&g ...
- PHP初入--添加内容到框框里并删除
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- MPLS VPN随堂笔记1
MPLS VPN 基础 1.MPLS vpn架构的特点 1.1.允许不同CE传递相同私网路由 1.2.SP内部(所有P路由器)不需要学习CE路由 1.3.无安全保障但有带宽保障(跟SP租用服务) 2. ...
- 转: 【Java并发编程】之三:线程挂起、恢复与终止的正确方法(含代码)
转载请注明出处:http://blog.csdn.net/ns_code/article/details/17095733 挂起和恢复线程 Thread 的API中包含两个被淘汰的方法,它们用 ...
- ceph存储引擎bluestore解析
原文链接:http://www.sysnote.org/2016/08/19/ceph-bluestore/ ceph后端支持多种存储引擎,以插件式的方式来进行管理使用,目前支持filestore,k ...
- 团队作业4--第一次项目冲刺(Alpha版本)7
一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 1.完成全部基础功能 2.完成一些小改进与优化 四.困难与问题 软件基本是可以运行并且正常使用,但还没有实战过,遇到的问题与困 ...
- 201521123040《Java程序设计》第7周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 参考资料: XMind 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代 ...
- 201521123034 《Java程序设计》第五周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 2. 书面作业 作业参考文件下载 代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件 ...