gcd 题解
gcd
Little White learned the greatest common divisor, so she plan to solve a problem: given \(x, n,\)
query \(\sum_{a=1}^n\sum_{b=1}^ngcd(x^a−1,x^b−1)\)Input
The first line of input is an integer T ( 1≤T≤300)
For each test case ,the single line contains two integers x and n ( 1≤x,n≤1000000)Output
For each testcase, output a line, the answer mod 1000000007
Sample Input
5
3 1
4 2
8 7
10 5
10 8Sample Output
2
24
2398375
111465
111134466
思路:
\(ans=\sum_{i=1}^n(x^i-1)\sum_{j=1}^{\lfloor \frac{n}{i} \rfloor}\phi(j)\)
证明:
设\(a>b\)
\((x^a-1,x^b-1)=(x^a-x^b,x^b-1)=(x^b(x^{a-b}-1),x^b-1)\)
\(\because (x^b,x^b-1)=1\)
\(\therefore (x^b(x^{a-b}-1),x^b-1)=(x^{a-b}-1,x^b-1)=(x^{a\%b-1},x^b-1)=(0,x^{(a,b)-1})=x^{(a,b)}-1\)
优化:
如果用朴素,时间复杂度\(O(Tn)\),会\(TLE\)
因为\(\lfloor \frac{n}{i} \rfloor\)只会有\(2\sqrt{n}\)个值,我们考虑整除分块和等比数列求和。
\(\mathfrak{Talk\ is\ cheap,show\ you\ the\ code.}\)
#pragma GCC optimize("Ofast")
#include<cstdio>
#include<algorithm>
using namespace std;
# define Type template<typename T>
# define read read1<int>()
Type inline T read1()
{
T t=0;
bool ty=0;
char k;
do k=getchar(),(k=='-')&&(ty=1);while('0'>k||k>'9');
do t=(t<<3)+(t<<1)+(k^'0'),k=getchar();while('0'<=k&&k<='9');
return ty?-t:t;
}
# define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout)
# define int long long
# define mod 1000000007ll
int l,r,phi[1000001],pri[1000001],s,m;
void work1(const int r=1000000)
{
phi[1]=1;
for(int i=1;i++^r;)
phi[i]=i;
for(int i=2;i<=r;++i)
{
if(phi[i]==i)
pri[++pri[0]]=i,phi[i]=i-1;
for(int j=1;j<=pri[0]&&pri[j]*i<=r;++j)
if(i%pri[j])phi[i*pri[j]]=phi[i]*(pri[j]-1);
else{phi[i*pri[j]]=phi[i]*pri[j];break;}
}
for(int i=0;i++^r;phi[i]=(phi[i]+phi[i-1])%mod);
}
int qkpow(int n,int m)
{
if(!m)return 1;
int t=qkpow(n,m>>1);
t=t*t%mod;
if(m&1)t=t*n%mod;
return t;
}
int que(int l,int r)
{
if(s==1)return r-l+1;
return qkpow(s,l)*(qkpow(s,r-l+1)-1)%mod*qkpow(s-1,mod-2)%mod;
}
void work()
{
s=read,m=read;
int ans=0,l=0,r=1;
while(l<m)
{
ans+=(que(l+1,r)-r+l)*(phi[m/r]*2-1)%mod;
ans%=mod;
l=r;
if(l==m)break;
r=m/(m/(l+1));
}
/*for(int i=0;i++^m;)
ans=(ans+(a[i]-1)*(phi[m/i]*2-1))%mod;*/
printf("%lld\n",(ans+mod)%mod);
}
signed main()
{
work1();
for(int T=read;T--;work());
return 0;
}
/*
(x^a-1,x^b-1)=(x^a-1,x^b-x^a)=(x^a-1,(x^(b-a)-1)*x^a)
=(x^a-1,x^(b-a)-1)=(x^a-1,x^(b%a)-1)
*/
gcd 题解的更多相关文章
- HDU5726:GCD——题解
题目:hdu的5726 (我原博客的东西,正好整理过来,属于st表裸题) (可以看出我当时有多么的菜--) 这道题写了一遍,然而蒟蒻的我的时间爆炸了-- 于是看了一下学长的代码(顺便在此处%一下学长) ...
- 洛谷 P2568 GCD 题解
原题链接 庆祝一下:数论紫题达成成就! 第一道数论紫题.写个题解庆祝一下吧. 简要题意:求 \[\sum_{i=1}^n \sum_{j=1}^n [gcd(i,j)==p] \] 其中 \(p\) ...
- BZOJ2820:YY的GCD——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2820 Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x& ...
- CC DGCD:Dynamic GCD——题解
https://vjudge.net/problem/CodeChef-DGCD https://www.codechef.com/problems/DGCD 题目大意: 给一颗带点权的树,两个操作: ...
- CH 4302 Interval GCD 题解
题意 给定一个长度为N的数列A,以及M条指令 (N≤5* 10^5, M<=10^5),每条指令可能是以下两种之一: "C l r d",表示把 A[l],A[l+1],-, ...
- [洛谷2257]YY的GCD 题解
整理题目转化为数学语言 题目要我们求: \[\sum_{i=1}^n\sum_{i=1}^m[gcd(i,j)=p]\] 其中 \[p\in\text{质数集合}\] 这样表示显然不是很好,所以我们需 ...
- 洛谷 P2257 YY的GCD 题解
原题链接 庆祝: 数论紫题 \(T4\) 达成! 莫比乌斯 \(T1\) 达成! yy 真是个 神犇 前记 之前我觉得: 推式子,直接欧拉筛,筛出个 \(\phi\),然后乱推 \(\gcd\) 就行 ...
- Codeforces Round #271 (Div. 2)题解【ABCDEF】
Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...
- 区间加值,区间gcd, 牛客949H
牛客小白月赛16H 小阳的贝壳 题目链接 题意 维护一个数组,支持以下操作: 1: 区间加值 2: 询问区间相邻数差的绝对值的最大值 3: 询问区间gcd 题解 设原数组为\(a\), 用线段树维护\ ...
随机推荐
- .Net Core 给WebApi接口返回值添加全局的日期格式化
public void ConfigureServices(IServiceCollection services) { services.AddMvc().AddJsonOptions(option ...
- 【开发笔记】- Idea启动Gradle报错:Warning:Unable to make the module: reading, related gradle configuration was not found. Please, re-import the Gradle project and try again
报错信息: Warning:Unable to make the module: reading, related gradle configuration was not found. Please ...
- C# 中using 用来释放资源的用法
using(...) {........} 定义了一个范围,等范围结束以后进行资源的释放. 例如: using(SqlConnection conn = new SqlConnection(" ...
- APS中生产计划排程模块的基本原理
高级计划系统(APS)作为ERP和MES的补充,用于协调物流.开发瓶颈资源和保证交货日期. APS包括需求和供应计划.运输和生产计划排程等各种供应链计划模块,本文主要介绍APS中生产计划排程模块的基本 ...
- Bugku 代码审计
0x01.extract变量覆盖 代码: <?php$flag='xxx';extract($_GET);if(isset($shiyan)){$content=trim(file_get_co ...
- 【转载】Gradle学习 第七章:Java快速入门
转载地址:http://ask.android-studio.org/?/article/22 7.1. The Java plugin(Java插件) As we have seen, Gradle ...
- flink 安装及wordcount
1.下载 http://mirror.bit.edu.cn/apache/flink/ 2.安装 确保已经安装java8以上 解压flink tar zxvf flink-1.8.0-bin-scal ...
- There is already an open DataReader associated with this Command which must be closed first
通常出现在嵌套查询数据库(比如在一个qry的遍历时,又进行了数据库查询) 通过在连接字符串中允许MARS可以轻松解决这个问题. 将MultipleActiveResultSets = true添加到连 ...
- LINUX中ORACLE 11.2.0.1 升级到11.2.0.4
11.2.0.4补丁号13390677,共7个文件,分别是 其中1&2是db,3是grid,4是client,5是gateways,6是example,7是deinstall 上传安装介质并解 ...
- 最常见Linux操作
命令 含义 cd /home/hadoop #把/home/hadoop设置为当前目录 cd .. #返回上一级目录 cd ~ #进入到当前Linux系统登录用户的主目录(或主文件夹).在 Linux ...