杭电2841 容斥定理求1到n之间和x互质的数的总数
2012-02-25 11:18
288 查看
第一次写容斥定理,完全不懂啊,,,弱爆了。。。。。话说这道题最后就是判断横坐标和纵坐标两个值是否互质,如果互质则可以看到,否则看不到。题目:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 593 Accepted Submission(s): 225
Problem Description
There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is standing at (0,0) point. He wonders how many trees he can see.
If two trees and Sherlock are in one line, Farmer Sherlock can only see the tree nearest to him.
Input
The first line contains one integer t, represents the number of test cases. Then there are multiple test cases. For each test case there is one line containing two integers m and n(1 ≤ m, n ≤ 100000)
Output
For each test case output one line represents the number of trees Farmer Sherlock can see.
Sample Input
Sample Output
ac代码:
Visible Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 593 Accepted Submission(s): 225
Problem Description
There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is standing at (0,0) point. He wonders how many trees he can see.
If two trees and Sherlock are in one line, Farmer Sherlock can only see the tree nearest to him.
Input
The first line contains one integer t, represents the number of test cases. Then there are multiple test cases. For each test case there is one line containing two integers m and n(1 ≤ m, n ≤ 100000)
Output
For each test case output one line represents the number of trees Farmer Sherlock can see.
Sample Input
2 1 1 2 3
Sample Output
1 5
ac代码:
#include <iostream> #include <cstdio> #include <vector> using namespace std; long long wid,len; long long prime(long long x){ vector<long long> ss; //先将x分解质因数 for(long long i=2;i*i<=x;++i){ if(x%i==0){ ss.push_back(i); while(x%i==0) x/=i; } } if(x>1) ss.push_back(x); //分解完毕 long long count=0; //转化成二进制判断二进制中有多少个1 for(long long i=1;i<(1<<ss.size());++i){ long long ones=0,mult=1; for(long long j=0;j<ss.size();++j){ if(i&(1<<j)){//按位与运算,判断1的个数 ones++; mult*=ss[j]; } } if(ones%2) count+=(wid/mult);//(wid/mult)为其中有多少个数是mult的倍数,即不互质 else count-=(wid/mult); } return wid-count; } int main(){ int numcase; scanf("%d",&numcase); while(numcase--){ long long sum=0; scanf("%lld%lld",&len,&wid); for(long long i=2;i<=len;++i){ sum+=(prime(i)); } printf("%lld\n",sum+wid); } return 0; }
相关文章推荐
- 杭电2841 容斥定理求1到n之间和x互质的数的总数
- 杭电2841 Visible Trees(容斥原理第二题)
- 杭电1695GCD【欧拉函数】【容斥定理】
- 杭电2001求两点之间的距离
- hdu 4135 Co-prime (组合数学:容斥定理+欧拉函数)
- HDU 3579 Hello Kiki 解题报告(中国剩余定理 非互质)
- 容斥原理求1到n与k互质个数
- 51nod oj 1262 扔球 【容斥定理||欧拉定理】
- Visible Trees HDU2841(容斥定理的简单扩展)
- 中国剩余定理 互质与非互质版本
- 中国剩余定理CRT (互质)
- Strange Way to Express Integers(中国剩余定理+不互质)
- UVa 11806 Cheerleaders (组合&逆向思维||容斥定理)
- 【杭电1222】互质
- HDU 1796How many integers can you find(简单容斥定理)
- Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理
- 杭电2096 今天刚刚学习的中国剩余定理,这个好用
- HDU5768:Lucky7(中国剩余定理 & 容斥)
- 容斥定理与鸽巢定理(抽屉定理)
- 容斥定理 | 初步