【题目链接】: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的更多相关文章

  1. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  2. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  3. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  4. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  5. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  6. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  7. 【hdu 1043】Eight

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...

  8. 【HDU 3068】 最长回文

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...

  9. 【HDU 4699】 Editor

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4699 [算法] 维护两个栈,一个栈放光标之前的数,另外一个放光标之后的数 在维护栈的同时求最大前缀 ...

随机推荐

  1. 使用Html5和Js进行拖动

    function init() {             var source = document.getElementById("dragme");             ...

  2. 曲线控件我一直用codeproject上的那几个(C++ 100款开源界面库)

    Alberl#23楼[楼主] 2013-11-04 11:47 Alberl @baita00引用看了大神的教程,真的不错,学习了很多东西,^_^.这节教程里,大神好像在找曲线控件,是吗?大神有什特殊 ...

  3. SQLite 常用函数

    SQLite 常用函数 参考: SQLite 常用函数 | 菜鸟教程http://www.runoob.com/sqlite/sqlite-functions.html SQLite 常用函数 SQL ...

  4. axis2调用webservice教训

    总结教训,axis2client调用WS接口时url不能加?wsdl,而用cxf调用时则要加上. 今天用axis2的RpcServerClient调用https的webservice接口,在设置完op ...

  5. 查找DLL,并复制出来

    Subst b: %windir%\assembly 执行完后,会发现硬盘分区多了个B盘,打开后看到了所有assembly下的DLL,于是在这里就搜到了Microsoft.ReportViewer.P ...

  6. Hadoop MapReduce编程 API入门系列之计数器(二十七)

    不多说,直接上代码. MapReduce 计数器是什么?    计数器是用来记录job的执行进度和状态的.它的作用可以理解为日志.我们可以在程序的某个位置插入计数器,记录数据或者进度的变化情况. Ma ...

  7. SQL学习整理

    SQL整理 SQL 对大小写不敏感! 一.对数据的操作 实现功能分类: 1. 增: 1.1 表存在,插入栏位: //插入新的行(按栏位的顺序插入) INSERT INTO Table_1 VALUES ...

  8. [ Database ] [ SQL Server ] SQL Server 很多不允許的操作解決方法

    說明可參考 https://blog.miniasp.com/post/2012/10/26/SQL-Server-Management-Studio-Prevent-saving-changes-t ...

  9. 【Oracle】查询当前SCN

    介绍两种方式: 一.sys用户下: select current_scn from v$database; select dbms_flashback.get_system_change_number ...

  10. SVN客户端安装 Linux

    1.下载 [maintain@HM16-213 software]$ wget http://subversion.tigris.org/downloads/subversion-deps-1.6.1 ...