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 ...
随机推荐
- python的white循环
# _*_coding:utf-8 _*_import datetimename = 'gyf'passd = 123count =0now = datetime.datetime.now()whil ...
- java面向对象(一)
[toc] 面向对象 我们都说java是面向对象的编程语言,那什么是面向对象呢?什么是类呢?什么是方法呢? 类.对象.方法 类是对象的抽象定义,对象是类的具体实例. 类:指的是一类东西,比如汽车,人类 ...
- 一台电脑 一起跑python2 python3
我习惯使用python2.7,命令都是使用的python和pip,这时候装了python3.4,首先到python3下修改python.exe,pythonw.exe为python3.exe,pyth ...
- C++中static关键字作用总结
1.先来介绍它的第一条也是最重要的一条:隐藏.(static函数,static变量均可) 当同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性.举例来说明.同时编译两个源文件 ...
- java的jar包加密
由于项目要求(虽然我觉得代码没什么机密可言...),写好的jar包需要做一定加密处理 这里提供两种办法,一种奇葩,一种通用 1. 直接修改jar文件: 具体步骤: 在代码中插入一段不会运行的到的代码 ...
- DFS和BFS(无向图)Java实现
package practice; import java.util.Iterator; import java.util.Stack; import edu.princeton.cs.algs4.* ...
- TX2017秋招笔试题之编码
问题描述: 假定一种编码的编码范围是a ~ y的25个字母,从1位到4位的编码,如果我们把该编码按字典序排序,形成一个数组如下: a, aa, aaa, aaaa, aaab, aaac, - -, ...
- 初学者入门web前端:C#基础知识:函数
入行前端对函数的掌握程度有可能直接影响以后工作的效率,使用函数可以高效的编写编码,节省时间,所以我整理了C#中最基础的函数知识点,虽然我在学习中 遇到很多问题,但是只要能够解决这些问题,都是好的. 一 ...
- vue中数据双向绑定的实现原理
vue中最常见的属v-model这个数据双向绑定了,很好奇它是如何实现的呢?尝试着用原生的JS去实现一下. 首先大致学习了解下Object.defineProperty()这个东东吧! * Objec ...
- Python3 多线程的两种实现方式
最近学习 Python3 ,希望能掌握多线程的使用,在此做个笔记.同时也希望Python 牛人指点错误.关于线程的概念,前面简单总结了一下 java 的多线程,传送门:java 多线程概念,三种创建多 ...