【hdu 4135】Co-prime
【题目链接】:http://acm.hdu.edu.cn/showproblem.php?pid=4135
【题意】
让你求出[a..b]这个区间内和N互质的数的个数;
【题解】
利用前缀和,求出[1..a-1]和a[1..b]这两个区间内和N互质的数的个数;
方法是;
从小到大求出N的所有因子xi;
然后从大到小枚举每个因子xi;
可以求出[1..t]内和N的最大公因数为xi的个数设为f[xi];
(减去大于xi且为xi的倍数的y就好,即f[xi]-=f[y],一开始f[xi]=t/xi);
(因为显然f[y]是那些虽然是xi的倍数,但gcd不为xi的数)
最后也就能顺理成章的求出f[1]了
【Number Of WA】
1(没开LL,QAQ)
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 310;
int n;
LL a,b,f[N];
vector <int> v1,v2;
LL get_ans(LL ma)
{
int len = v1.size();
rep1(i,0,len-1)
f[i+1] = ma/v1[i];
rep2(i,len,1)
rep1(j,i+1,len)
if (v1[j-1]%v1[i-1]==0)
f[i]-=f[j];
return f[1];
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
int T;
cin >> T;
rep1(ii,1,T)
{
v1.clear(),v2.clear();
cin >> a >> b >> n;
int len = sqrt(n);
rep1(i,1,len)
if (n%i==0)
{
int x = i,y = n/i;
v1.pb(x);
if (x!=y) v2.pb(y);
}
int len2 = v2.size();
rep2(i,len2-1,0) v1.pb(v2[i]);
LL t1 = get_ans(b),t2 = get_ans(a-1);
cout <<"Case #"<<ii<<": ";
cout << t1-t2<<endl;
}
return 0;
}
【hdu 4135】Co-prime的更多相关文章
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
- 【hdu 1043】Eight
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...
- 【HDU 3068】 最长回文
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...
- 【HDU 4699】 Editor
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4699 [算法] 维护两个栈,一个栈放光标之前的数,另外一个放光标之后的数 在维护栈的同时求最大前缀 ...
随机推荐
- Narrow Art Gallery
Time Limit: 4000ms, Special Time Limit:10000ms, Memory Limit:65536KB Total submit users: 11, Accepte ...
- map-reduce入门
map-reduce入门 近期在改写mahout源代码,感觉自己map-reduce功力不够深厚,因此打算系统学习一下. map-reduce事实上是一种编程范式,从统计词频(wordCount)程序 ...
- 坚向的ViewPager,上下滑动的组件,android上下滑动 VerticalPager
package com.zhulin.android.atools; import android.content.Context; import android.util.AttributeSet; ...
- linux下udev简介【转】
本文转载自:http://blog.csdn.net/skyflying2012/article/details/9364555 一.关于Udev u即user space,dev是device,通过 ...
- PHP检测输入数据是否合法常用的类(转)
<?php class Fun{ function isEmpty($val) { if (!is_string($val)) return false; //是否是字符串类型 if (empt ...
- new一个接口
首先我们先看看接口的定义: 接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方 ...
- C# 开放式并发冲突报错处理
1.调用DataSet.GetChanges()获取数据源中改变的数据 var data = ViewData.GetChanges() as ReleaseData; 2.为新增的数据 data 加 ...
- ROS-tutorials-launch-查看日志
前言:launch文件的作用是一次可以启动多个节点. 1.新建launch文件 在chapter2_tutorials包下新建launch文件夹,并在launch文件夹下新建chapter2.laun ...
- poj 1161 Floyd+枚举
题意是: 给出n个点,围成m个区域.从区域到另一个区域间需穿过至少一条边(若两区域相邻)——边连接着两点. 给出这么一幅图,并给出一些点,问从这些点到同一个区域的穿过边数最小值. 解题思路如下: 将区 ...
- Nginx 404 500
Nginx反向代理自定义404错误页面 http中添加 proxy_intercept_errors on; server中添加 error_page 404 = https://www.longda ...