Visible Trees HDU - 2841

Visible Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4331    Accepted Submission(s): 1991

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

Source

2009 Multi-University Training Contest 3 - Host by WHU

在同一条直线(y = kx (k为自然数))上的点只能看见最前面的 最前面的点的 y 和 x 肯定互质

所以就变成了 求m * n 这个区域中互质的 x 与 y 的对数

对于每一个1 ~ n 求 1 ~ m中有多少个与之互质的数  加起来就好了

tip:容斥求出与之有公因子的数 然后m - 这个数 就是与之互质的数的个数了

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 10010, INF = 0x7fffffff;
int prime[maxn];

int get_cnt(int n, int m)
{
    int ans = 0;
    for(int i = 2; i * i <= n; i++)
    {
        if(n % i) continue;
        while(n % i == 0) n /= i;
        prime[ans++] = i;
    }
    if(n != 1) prime[ans++] = n;
    int res = 0;
    for(int i = 1; i < (1 << ans); i++)
    {
        int tmp = 1, cnt2 = 0;
        for(int j = 0; j < ans; j++)
        {
            if(((i >> j) & 1) == 0) continue;
            tmp *= prime[j];
            cnt2++;
        }
        if(cnt2 & 1) res += m / tmp;
        else res -= m / tmp;
    }
    return m - res;
}

int main()
{
    int n, m, t;
    rd(t);
    while(t--)
    {
        rd(n), rd(m);
        LL sum = 0;
        rap(i, 1, n)
        {
            sum += get_cnt(i, m);
        }

        plld(sum);
    }

    return 0;
}

Visible Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4331    Accepted Submission(s): 1991

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

Source

2009 Multi-University Training Contest 3 - Host by WHU

原文地址:https://www.cnblogs.com/WTSRUVF/p/10321261.html

时间: 2024-10-19 21:30:18

Visible Trees HDU - 2841的相关文章

HDU 2841 Visible Trees

Visible Trees Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 284164-bit integer IO format: %I64d      Java class name: Main There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherl

hdu 2841 Visible Trees(计数问题)

题目链接:hdu 2841 Visible Trees 题目大意:一个n?m的矩阵,每个整数点上有树,人站在(0,0)点,问可以看见多少棵树. 解题思路:和uva1393是一道相同类型的题目,只不过这道题目的n比较大,不能预处理.必须用另外一种方法. 将矩阵按照(0,0)和(n,m)两天连成的直线分成两部分,分别计算,但是(n,m)这条线被计算了两次,于是减掉1. dp[i]表示这一列上有多少个点是可以被看见的,因为矩阵被对半分了,所以点的个数为m?in,同时还要计算每次看见的点会把后面的给挡住

[思维+容斥原理] hdu 2841 Visible Trees

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2841 Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1337    Accepted Submission(s): 552 Problem Description There are many trees f

hdu 2841 Visible Trees 容斥原理

Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) 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 ma

hdu 2841 Visible Trees【容斥原理】

Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1602    Accepted Submission(s): 661 Problem Description There are many trees forming a m * n grid, the grid starts from (1,1). Farm

hdu 4135 Co-prime +hdu 2841 Visible Trees(容斥原理)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2263    Accepted Submission(s): 847 Problem Description Given a number N, you are asked to count the number of integers between A and B

HDU 2841 Visible Trees(数论)

题目大意:给你一个m*n的方格,方格从(1,1)开始.每个点上有一棵树,然后让你从(0,0)点向下看,问你能看见几颗树. 解题思路:如果你的视线被后面的树和挡住的话以后在这条线上的树你是都看不见的啊.挡住的话就是这个小的方格内对角线的连线过顶点,如图: 建设A为(0,0)如果B能遮挡住C,那么B与C组成的矩形中nx, mx是不互质的. 所以x从1开始枚举n如果m中某一项与x互质那么就会有一个格子显示出来,所以这里需要找的是1-n中与枚举的x互质的个数.这里的话我们可以用到分解质因数,然后用状态压

hdu2848 Visible Trees (容斥定理)

题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看到多少个点. 知识点: 容斥原理:(容许) 先不考虑重叠的情况,把包含于某条件中的所有对象的数目先计算出来,(排斥)然后再把计数时重复计算的数目排斥出去,使得计算的结果既无遗漏又无重复. 公式:          奇加偶减 一般求互质个数若用欧拉函数不好解决,则从反面考虑,用容斥. 模板: void

Visible Trees(hdu2841)

Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2462    Accepted Submission(s): 1032 Problem Description There are many trees forming a m * n grid, the grid starts from (1,1). Farm