hdu 4983 Goffi and GCD(数论)
题目大意:求有多少对元组满足题目中的公式。
解题思路:
- n = 1或者k=2时:答案为1
- k > 2时:答案为0(n≠1)
- k = 1时:须要计算,枚举n的因子。令因子k=gcd(n−a,n,
那么还有一边的gcd(n−b,n)=nk才干满足相乘等n。满足k=gcd(n−a,n)的a的个数即为ϕ(n/s),欧拉有o(n‾‾√的算法
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 1e5;
const int MOD = 1e9+7;
typedef long long ll;
ll ans;
int N, K, M;
int np, pri[maxn+5], vis[maxn+5];
int nf, fact[maxn+5], coun[maxn+5];
void prime_table (int n) {
np = 0;
memset(vis, 0, sizeof(vis));
for (int i = 2; i <= n; i++) {
if (vis[i])
continue;
pri[np++] = i;
for (int j = 2 * i; j <= n; j += i)
vis[j] = 1;
}
}
void div_factor(int n) {
nf = 0;
for (int i = 0; i < np; i++) {
if (n % pri[i] == 0) {
coun[nf] = 0;
while (n % pri[i] == 0) {
coun[nf]++;
n /= pri[i];
}
fact[nf++] = pri[i];
}
}
if (n != 1) {
coun[nf] = 1;
fact[nf++] = n;
}
}
int euler_phi(int n) {
int m = (int)sqrt((double)n+0.5);
int ret = n;
for (int i = 2; i <= m; i++) {
if (n % i == 0) {
ret = ret / i * (i-1);
while (n%i==0)
n /= i;
}
}
if (n > 1)
ret = ret / n * (n - 1);
return ret;
}
ll add (int s) {
ll a = euler_phi(N/s);
ll b = euler_phi(s);
ll ret = a * b * 2;
if (s == N / s)
ret /= 2;
return ret;
}
void dfs (int s, int d) {
if (s > M)
return;
if (d == nf) {
ans = (ans + add(s)) % MOD;
return;
}
for (int i = 0; i <= coun[d]; i++) {
dfs(s, d+1);
s *= fact[d];
}
}
int solve () {
ans = 0;
M = (int)sqrt((double)N);
div_factor(N);
dfs (1, 0);
return ans % MOD;
}
int main () {
prime_table(maxn);
while (scanf("%d%d", &N, &K) == 2) {
if (N == 1)
printf("1\n");
else if (K > 2)
printf("0\n");
else if (K == 2)
printf("1\n");
else
printf("%d\n", solve());
}
return 0;
}
hdu 4983 Goffi and GCD(数论)的更多相关文章
- HDU 4983 Goffi and GCD(数论)
HDU 4983 Goffi and GCD 思路:数论题.假设k为2和n为1.那么仅仅可能1种.其它的k > 2就是0种,那么事实上仅仅要考虑k = 1的情况了.k = 1的时候,枚举n的因子 ...
- hdu 4983 Goffi and GCD(欧拉函数)
Problem Description Goffi is doing his math homework and he finds an equality on his text book: gcd( ...
- HDU 4983 Goffi and GCD
题目大意:给你N和K,问有多少个数对满足gcd(N-A,N)*gcd(N-B,N)=N^K.题解:由于 gcd(a, N) <= N,于是 K>2 都是无解,K=2 只有一个解 A=B=N ...
- 【HDOJ】4983 Goffi and GCD
题意说的非常清楚,即求满足gcd(n-a, n)*gcd(n-b, n) = n^k的(a, b)的不同对数.显然gcd(n-a, n)<=n, gcd(n-b, n)<=n.因此当n不为 ...
- HDU 4981 Goffi and Median(水)
HDU 4981 Goffi and Median 思路:排序就能够得到中间数.然后总和和中间数*n比較一下就可以 代码: #include <cstdio> #include <c ...
- HDU 4982 Goffi and Squary Partition(推理)
HDU 4982 Goffi and Squary Partition 思路:直接从全然平方数往下找,然后推断是否能构造出该全然平方数,假设能够就是yes,假设都不行就是no.注意构造时候的推断,因为 ...
- hdu 5869 区间不同GCD个数(树状数组)
Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)
CA Loves GCD Accepts: 64 Submissions: 535 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 2 ...
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
随机推荐
- java 读入文件 FileInputStream
package com.mkyong.io; import java.io.File; import java.io.FileInputStream; import java.io.IOExcepti ...
- onvif 开发中的一些重要函数介绍
➤soap结构中count(soap->count)成员 soap结构中count(soap->count)成员记录的是http协议中Content-Length的数值. ➤keep_al ...
- Tomcat+Apache集群方案
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha # environment slash for Windows(反斜杠代表Windows ...
- 51nod1031 骨牌覆盖 组合数学
不难发现,只有$1 * 2, 2 * 2$两种方法 因此,设$f[i]$表示填满$1 - i$的方案数 那么有$f[i] = f[i - 1] + f[i - 2]$,其实就是斐波那契数列.... 复 ...
- [P3759][TJOI2017]不勤劳的图书管理员(分块+树状数组)
题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生 这两本书页数的和的厌烦度.现在有n本被打乱顺序的书 ...
- bzoj 2251
第一道后缀数组 后缀数组要维护三个数组:sa(suffix array), rk(rank)和ht(height). 含义分别是: sa[i]:将后缀按照字典序排序后,第i大的后缀的起始位置. rk[ ...
- bzoj 2002 LinkCutTree
我的第一道LCT题(居然1A,O(∩_∩)O哈哈~) 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 大概题意: 给一颗有根树,维护每个 ...
- 某DP题目4
题意 有两个栈分别有n和m个数,每次从任意栈中取出一个数,令k为不同输出序列的总数,其中第i种输出序列的产生方式有ai个,求Σai2. n <= 500 分析 此题是关于ai2转换.咋一看此题好 ...
- ACM -- 算法小结(四)KMP(POJ3461)
KMP -- POJ3461解题报告 问题描述:给出字符串P和字符串T,问字符串P在字符串T中出现的次数 Sample Input 3 BAPC BAPC AZA AZAZAZA VERDI ...
- iOS Core Animation 动画 入门学习(一)基础
reference:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreAnimation_guide ...