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,这里不一定是准确值,如果在这个区间内改变 ...
随机推荐
- 表视图(UITableView)与表视图控制器(UITableViewController)
表视图(UITableView)与表视图控制器(UITableViewController)其实是一回事. 表视图控制器是一种只能显示表视图的标准视图控制器,可在表视图占据整个视图时使用这种控制器.虽 ...
- urllib2 调用salt restapi
1获取token #_*_coding:utf8_*_ import urllib2 import json def get_token(): url='http://10.20.32.86:8000 ...
- 【BZOJ 3561】 3561: DZY Loves Math VI (莫比乌斯,均摊log)
3561: DZY Loves Math VI Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 205 Solved: 141 Description ...
- bzoj1116 [POI2008]CLO 边双联通分量
只需对每个联通块的$dfs$树检查有没有返租边即可 复杂度$O(n + m)$ #include <cstdio> #include <cstring> using names ...
- 最短路径:我的理解--SPFA算法
SPFA算法 求单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm. 最短路径快速算法-SPFA算法是西南交通大学段凡丁于1994年发表的. 适用范围:给定 ...
- C#中&和&&,|和||区别
当两者都为逻辑运算符时. 其实没什么差别. &&和||当已经确定结果时,不会对第二个操作数求值.也不知道什么情况会用到这个差别.做个笔记好了. http://blog.csdn.net ...
- 实战-130W表增加字段耗时
工作需要对130W的表增加字段,因为是操作线上数据库,所以提前在本地调查下耗时. 首先建表: CREATE TABLE `alter_cloumn_test` ( `id` int(11) unsig ...
- linux基础命令学习 (七)压缩解压
一.tar tar主要用来压缩和解压文件 语法: tar [主选项+辅选项] 文件或者目录 主选项: c 创建新的档案文件.如果用户想备份一个目录或是一些文件,就要选择这个选项.相当于打包. x 从档 ...
- TCP长连接与短链接
1. TCP连接 当网络通信时采用TCP协议时,在真正的读写操作之前,server与client之间必须建立一个连接,当读写操作完成后,双方不再需要这个连接时它们可以释放这个连接,连接的建立是需要三次 ...
- Unity3D架构设计NavMesh寻路(未完待续)
国庆闲来没事把NavMesh巩固一下.以Unity3D引擎为例写一个底层c# NavMesh寻路.由于Unity3D中本身自带的NavMesh寻路不能非常好的融入到游戏项目其中,所以重写一个NavMe ...