hdu6390GuGuFishtion【数论】

时间:2022-10-01 10:55:32

GuGuFishtion

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1204    Accepted Submission(s): 459

Problem Description

Today XianYu is too busy with his homework, but the boring GuGu is still disturbing him!!!!!!

At the break time, an evil idea arises in XianYu's mind.

‘Come on, you xxxxxxx little guy.’

‘I will give you a function ϕ(x) which counts the positive integers up to x that are relatively prime to x.’

‘And now I give you a fishtion, which named GuGu Fishtion, in memory of a great guy named XianYu and a disturbing and pitiful guy GuGu who will be cooked without solving my problem in 5 hours.’

‘The given fishtion is defined as follow:

Gu(a,b)=ϕ(ab)ϕ(a)ϕ(b)

And now you, the xxxxxxx little guy, have to solve the problem below given m,n,p.’

(∑a=1m∑b=1nGu(a,b))(modp)

So SMART and KINDHEARTED you are, so could you please help GuGu to solve this problem?

‘GU GU!’ GuGu thanks.

Input

Input contains an integer T indicating the number of cases, followed by T lines. Each line contains three integers m,n,p as described above.

1≤T≤3

1≤m,n≤1,000,000

max(m,n)<p≤1,000,000,007

And given p is a prime.

Output

Please output exactly T lines and each line contains only one integer representing the answer.

Sample Input


 

1 5 7 23

Sample Output


 

2

Source

2018 Multi-University Training Contest 7

Recommend

chendu   |   We have carefully selected several similar problems for you:  6408 6407 6406 6405 6404

先去学习了一下欧拉函数

这道题根据欧拉函数的定义hdu6390GuGuFishtion【数论】化简可以得到

hdu6390GuGuFishtion【数论】

因此对于题目要求的hdu6390GuGuFishtion【数论】

我们需要先计算每一个i/φ(i)的值, 再计算gcd() = i的数对的数目

先预处理出所有的φi

对于一个数i,在a∈[1,n],b∈[1,m]a∈[1,n],b∈[1,m]的范围内,设f[i]为gcd为(i,2i,3i...)的对数设f[i]为gcd为(i,2i,3i...)的对数

显然 : f[i]=[n/i]∗[m/i]f[i]=[n/i]∗[m/i]

那么我们从大到小维护f[i]f[i],因为我们要的是 gcd=igcd=i 的对数,所以要把 gcd=2igcd=2i 的情况减去【预处理】


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#define inf 1e18
using namespace std; int t, n, m;
const int maxn = 1000005;
long long f[maxn], a[maxn], p;
long long cnt[maxn]; long long is[maxn], phi[maxn], pri[maxn], nump;
/*
特性 :
1.若a为质数,phi[a]=a-1;
2.若a为质数,b mod a=0,phi[a*b]=phi[b]*a
3.若a,b互质,phi[a*b]=phi[a]*phi[b](当a为质数时,if b mod a!=0 ,phi[a*b]=phi[a]*phi[b])
*/
void make()
{
memset(phi, 0, sizeof(phi));
memset(f, 0, sizeof(f));
phi[1] = 1;
for(int i = 2; i <= maxn; i++){
if(!is[i]){//i是素数
pri[++nump] = i;
phi[i] = i - 1;
}
for(int j = 1; j <= nump && pri[j] * i < maxn; j++){//筛
is[pri[j] * i] = 1;
if(i % pri[j] == 0){
phi[pri[j] * i] = phi[i] * pri[j];
break;
}
else phi[pri[j] * i] = phi[i] *(pri[j] - 1);
}
} cnt[1] = 1;
for(int i = 1; i < maxn; i++){
for(int j = 2 * i; j < maxn; j += i){
cnt[j] -= cnt[i];
}
}
} void deal()
{
f[1] = 1;
for(int i = 2; i <= min(n, m); i++)
f[i] = f[p % i] * (p - p / i) % p;
for(int i = 1; i <= min(n, m); i++){
a[i] = (long long)i * f[phi[i]] % p;
}
} long long get(int n, int m)
{
long long ans = 0;
for(int i = 1; i <= min(n, m); i++){
ans+= (long long ) cnt[i] * (n / i) * (m / i);
ans %= p;
}
return ans;
} int main()
{
make();
cin>>t;
while(t--){
scanf("%d%d%lld", &m, &n, &p);
deal();
long long ans = 0;
for(int i = 1; i <= min(n, m); i++){
ans += (long long )a[i] * get(n / i, m / i);
ans %= p;
}
printf("%lld\n", ans);
}
return 0;
}

hdu6390GuGuFishtion【数论】的更多相关文章

  1. Codeforces Round &num;382 Div&period; 2【数论】

    C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...

  2. NOIP2014 uoj20解方程 数论(同余)

    又是数论题 Q&A Q:你TM做数论上瘾了吗 A:没办法我数论太差了,得多练(shui)啊 题意 题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, ...

  3. 数论学习笔记之解线性方程 a&ast;x &plus; b&ast;y &equals; gcd&lpar;a&comma;b&rpar;

    ~>>_<<~ 咳咳!!!今天写此笔记,以防他日老年痴呆后不会解方程了!!! Begin ! ~1~, 首先呢,就看到了一个 gcd(a,b),这是什么鬼玩意呢?什么鬼玩意并不 ...

  4. hdu 1299 Diophantus of Alexandria &lpar;数论&rpar;

    Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  5. 【BZOJ-4522】密钥破解 数论 &plus; 模拟 ( Pollard&lowbar;Rho分解 &plus; Exgcd求逆元 &plus; 快速幂 &plus; 快速乘)

    4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 290  Solved: 148[Submit][Status ...

  6. bzoj2219&colon; 数论之神

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  7. hdu5072 Coprime (2014鞍山区域赛C题)(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5072 题意:给出N个数,求有多少个三元组,满足三个数全部两两互质或全部两两不互质. 题解: http://dty ...

  8. ACM&colon; POJ 1061 青蛙的约会 -数论专题-扩展欧几里德

    POJ 1061 青蛙的约会 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu  Descr ...

  9. 数论初步&lpar;费马小定理&rpar; - Happy 2004

    Description Consider a positive integer X,and let S be the sum of all positive integer divisors of 2 ...

随机推荐

  1. 《CLR&period;via&period;C&num;第三版》第二部分第6,7章节读书笔记&lpar;三&rpar;

    第6章讲的是类型和成员基础 重要认知:虚方法 虚方法的设计原则:设计一个类型时,应尽量减少所定义的虚方法的数量. 首先,调用虚方法的速度比调用非虚方法慢. 其次,JIT编译器不能内嵌虚方法,这进一步影 ...

  2. java并发编程学习:用 Semaphore (信号量)控制并发资源

    并发编程这方面以前关注得比较少,恶补一下,推荐一个好的网站:并发编程网 - ifeve.com,上面全是各种大牛原创或编译的并发编程文章. 今天先来学习Semaphore(信号量),字面上看,根本不知 ...

  3. Linux变量

    变量:(大的分为环境变量与本的变量) 本地变量: 本地变量在用户现在的shell生命期的脚本中使用.例如,本地变量file-name="loop.doc",这个值只在用户当前she ...

  4. 软件测试之 LoadRunner安装&bsol;破解&bsol;汉化

    一.下载 LoadRunner下载地址:http://kuai.xunlei.com/d/QRNIUASALOIE 二. 安装 1.启动安装程序 运行setup.exe,点击“LoadRunner完整 ...

  5. Postman 基本操作学习

    History 所有使用postman发送的request都会保存在这里.点击之后会在当前Tab打开. 参考: Requests History Environments 这里用来设定当前reques ...

  6. python运算符使用规律

    #conding=utf-8 #优先级使用规律#1.一般情况下是左右结合print 4+6+5*6+6 #2.出现赋值的时候一般是右结合a=8+91print a #优先级记忆口诀'''函数寻址下标1 ...

  7. java BigDecimal的使用和四舍五入及格式规范(精准数据)

    • Java中的简单浮点数类型float和double不能够进行运算.不光是Java,在其它很多编程语言中也有这样的问题. 如果我们编译运行下面这个程序会看到什么? public   class  T ...

  8. 每天一个linux命令&lpar;34&rpar;--top命令

    今天给领导发邮件,我这边虽然显示发出去了,但是他那边一直没收到,结果我以为我发了,他又一直在那边等结果.所以说,以后要另外发个信息或者QQ微信之类的说一声. top命令是Linux 下常用的性能分析工 ...

  9. SAP S4系统创建Customer和Vendor的BAPI

    对应的BAPI是:RFC_CVI_EI_INBOUND_MAIN SAP 又调皮了,又不安常理出牌!

  10. AI繁荣下的隐忧——Google Tensorflow安全风险剖析

    本文由云+社区发表 作者:[ Tencent Blade Team ] Cradmin 我们身处一个巨变的时代,各种新技术层出不穷,人工智能作为一个诞生于上世纪50年代的概念,近两年出现井喷式发展,得 ...