GVKun编程网logo

Countable Rational Numbers

26

本文将带您了解关于CountableRationalNumbers的新内容,另外,我们还将为您提供关于315.CountofSmallerNumbersAfterSelf、c#–如何使用Reflect

本文将带您了解关于Countable Rational Numbers的新内容,另外,我们还将为您提供关于315. Count of Smaller Numbers After Self、c# – 如何使用Reflection在IEnumerable上调用System.Linq.Enumerable.Count <>?、C#IEnumerable.Count()抛出IndexOutOfRangeException、Cannot create PoolableConnectionFactory (Invalid number format for port number)的实用信息。

本文目录一览:

Countable Rational Numbers

Countable Rational Numbers

分享图片

 

分享图片

 

 

分享图片

 

分享图片

 

 

分享图片

#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
const ll maxn=1000005;
int prime[maxn/10],phi[maxn];
bool vis[maxn];
int tot;
 
inline int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}
 
void init()
{
    phi[1]=1;
    for (ll i=2; i<maxn; ++i)
    {
        if (!vis[i])
        {
            prime[tot++]=i;
            phi[i]=i-1;
        }
        for (ll j=0; j<tot&&1ll*prime[j]*i<maxn; j++)
        {
            vis[prime[j]*i]=1;
            if(i%prime[j]==0)
            {
                phi[prime[j]*i]=phi[i]*prime[j];
                break;
            }
            phi[prime[j]*i]=phi[i]*phi[prime[j]];
        }
    }
}
 
inline bool check(ll xx,ll yy,ll x,ll y)
{
    if(max(abs(xx),abs(yy))!=max(abs(x),abs(y)))
    {
        return max(abs(xx),abs(yy))<max(abs(x),abs(y));
    }
    if(yy!=y)
    {
        return yy<y;
    }
    return xx*yy>x*y;
}
 
int main()
{
    init();
    ll _;
    scanf("%lld",&_);
    ll x,y;
    ll k;
    ll ans=0;
    while (_--)
    {
        ll xx,yy;
        ans=0;
        scanf("%lld%lld",&y,&x);
        if(y==0&&x==1){
            printf("2\n");
            continue;
        }
        if (x<=0||x*y==0||gcd(x,abs(y))!=1)
        {
            printf("0\n");
            continue;
        }
        if(max(x,abs(y))<=2)
        {
            if(x==2&&y==1)
            {
                puts("6");
            }
            else if(x==1&& y==2)
            {
                puts("7");
            }
            else if(x==1 &&y==1)
            {
                puts("3");
            }
            else if(x==1&&y==0)
            {
                puts("2");
            }
            else if(x==1&&y==-1)
            {
                puts("1");
            }
            else if(x==2&& y==-1)
            {
                puts("5");
            }
            else if(x==1&&y==-2)
            {
                puts("4");
            }
            continue;
        }
 
        k=max(x,abs(y));
        if (k>=3)
        {
            ans=3;
            for(ll i=2; i<=k-1; ++i)
                ans+=4*phi[i];
        }
        for (ll i=1; i<=k; ++i)
        {
            if (i>1&&k%i==0) continue;
            xx=k;
            yy=i;
            if (gcd(xx,yy)==1&&check(xx,yy,x,y))
                ans++;
            xx=k;
            yy=-i;
            if (gcd(xx,abs(yy))==1&&check(xx,y))
                ans++;
 
            yy=k;
            xx=i;
            if (gcd(xx,y))
                ans++;
 
            yy=-k;
            xx=i;
            if (gcd(xx,y))
                ans++;
        }
        printf("%lld\n",ans+1);
    }
    return 0;
}

315. Count of Smaller Numbers After Self

315. Count of Smaller Numbers After Self

315. Count of Smaller Numbers After Self

题目链接:https://leetcode.com/problems...

divide and conquer的题,用bst来做,这种求有多少smaller的题一般都是bst。node里多加一个信息:size表示以node为subtree的节点数。

public class Solution {
    public List<Integer> countSmaller(int[] nums) {
        /* binary search tree 
         */
        int n = nums.length;
        LinkedList<Integer> res = new LinkedList();
        if(n == 0) return res;
        
        res.add(0);
        Node root = new Node(nums[n-1]);
        for(int i = n - 2; i >= 0; i--) {
            res.addFirst(findSmaller(root, nums[i]));
        }
        return res;
    }
    
    private int findSmaller(Node root, int value) {
        int res = 0;
        while(root != null) {
            root.size += 1;
            if(root.val < value) {
                // add root and all left nodes
                res += 1 + (root.left == null ? 0 : root.left.size);
                if(root.right == null) {
                    root.right = new Node(value);
                    break;
                }
                root = root.right;
            }
            else {
                if(root.left == null) {
                    root.left = new Node(value);
                    break;
                }
                root = root.left;
            }
        }
        return res;
    }
    
    class Node {
        int val;
        Node left;
        Node right;
        // count the size of this subtree
        int size;
        Node(int val) { this.val = val; this.size = 1; }
    }
}

binary index tree也可以做,因为是统计有多少smaller的,其实就是求从最小值到nums[i] - 1的sum。tree的index是nums[i],要做一个映射,把nums[i]的值映射到[1, # of unique numbers in nums]之间。所以先把array给sort一下,用一个map来做映射。

public class Solution {
    public List<Integer> countSmaller(int[] nums) {
        /* binary index tree 
         */
        // reflection first, key: nums[i], value: order
        Map<Integer, Integer> map = new HashMap();
        int[] sorted = Arrays.copyOf(nums, nums.length);
        Arrays.sort(sorted);
        // record the order
        int idx = 1;
        for(int i = 0; i < nums.length; i++) {
            if(!map.containsKey(sorted[i])) map.put(sorted[i], idx++);
        }
        // range will be [1, idx]
        BIT t = new BIT(idx);
        LinkedList<Integer> res = new LinkedList();
        for(int i = nums.length - 1; i >= 0; i--) {
            int sum = t.sum(map.get(nums[i]) - 1);
            res.addFirst(t.sum(map.get(nums[i]) - 1));
            t.add(map.get(nums[i]), 1);
        }
        return res;
    }
    
    class BIT {
        int[] tree;
        int n;
        BIT(int n) { this.n = n; tree = new int[n]; }
        // sum the smaller elements
        protected int sum(int i) {
            int res = 0;
            while(i > 0) {
                res += tree[i];
                i -= (i & -i);
            }
            return res;
        }
        
        protected void add(int i, int val) {
            while(i < n) {
                tree[i] += val;
                i += (i & -i);
            }
        }
    }
}

还有merge sort的方法,参考discussion:
https://discuss.leetcode.com/...

c# – 如何使用Reflection在IEnumerable上调用System.Linq.Enumerable.Count <>?

c# – 如何使用Reflection在IEnumerable上调用System.Linq.Enumerable.Count <>?

我有一堆IEnumerable集合,确切的数量和类型经常更改(由于自动代码生成).

它看起来像这样:

public class MyCollections {
    public System.Collections.Generic.IEnumerable<SomeType> SomeTypeCollection;
    public System.Collections.Generic.IEnumerable<OtherType> OtherTypeCollection;
    ...

在运行时,我想确定每个Type和它的计数,而不必在每次代码生成后重写代码.所以我正在寻找使用反射的通用方法.我正在寻找的结果是这样的:

MyType: 23
OtherType: 42

我的问题是我无法想象如何正确调用Count方法.这是我到目前为止:

// Handle to the Count method of System.Linq.Enumerable
        MethodInfo countMethodInfo = typeof(System.Linq.Enumerable).getmethod("Count",new Type[] { typeof(IEnumerable<>) });

        PropertyInfo[] properties = typeof(MyCollections).GetProperties();
        foreach (PropertyInfo property in properties)
        {
            Type propertyType = property.PropertyType;
            if (propertyType.IsGenericType)
            {
                Type genericType = propertyType.GetGenericTypeDeFinition();
                if (genericType == typeof(IEnumerable<>))
                {
                    // access the collection property
                    object collection = property.GetValue(someInstanceOfMyCollections,null);

                    // access the type of the generic collection
                    Type genericArgument = propertyType.GetGenericArguments()[0];

                    // make a generic method call for System.Linq.Enumerable.Count<> for the type of this collection
                    MethodInfo localCountMethodInfo = countMethodInfo.MakeGenericmethod(genericArgument);

                    // invoke Count method (this fails)
                    object count = localCountMethodInfo.Invoke(collection,null);

                    System.Diagnostics.Debug.WriteLine("{0}: {1}",genericArgument.Name,count);
                }
            }
        }

解决方法

如果你坚持不懈努力; p

变化:

>如何获取泛型方法的countMethodInfo
> Invoke的参数

代码(注意obj是我的MyCollections实例):

MethodInfo countMethodInfo = typeof (System.Linq.Enumerable).getmethods().Single(
        method => method.Name == "Count" && method.Isstatic && method.GetParameters().Length == 1);

    PropertyInfo[] properties = typeof(MyCollections).GetProperties();
    foreach (PropertyInfo property in properties)
    {
        Type propertyType = property.PropertyType;
        if (propertyType.IsGenericType)
        {
            Type genericType = propertyType.GetGenericTypeDeFinition();
            if (genericType == typeof(IEnumerable<>))
            {
                // access the collection property
                object collection = property.GetValue(obj,null);

                // access the type of the generic collection
                Type genericArgument = propertyType.GetGenericArguments()[0];

                // make a generic method call for System.Linq.Enumerable.Count<> for the type of this collection
                MethodInfo localCountMethodInfo = countMethodInfo.MakeGenericmethod(genericArgument);

                // invoke Count method (this fails)
                object count = localCountMethodInfo.Invoke(null,new object[] {collection});

                System.Diagnostics.Debug.WriteLine("{0}: {1}",count);
            }
        }
    }

C#IEnumerable.Count()抛出IndexOutOfRangeException

C#IEnumerable.Count()抛出IndexOutOfRangeException

分组集合时,我有以下情况:
var result = data.GroupBy(x => x.Name.Split(new char[] { '-' })[1].Trim());

其中数据变量的类型为ObservableCollection< Data>

当我检查

if(result.Count()>0)

它抛出一个IndexOutOfRangeException

当然这是因为字符串拆分操作会引发异常.

问题是:有没有办法检查分组的结果是否为空并避免异常?

解决方法

首先,Enumerable.Count只执行延迟执行的LINQ查询( GroupBy使用延迟执行,查看备注部分).所以伯爵不应该责怪这里.

您正在拆分 – 并在索引1处访问此数组,这是第二个项目.显然没有第二项,因为没有 – .所以它与null无关.

也许只有那些有第二个令牌的那个就足够了:

var result = data
.Select(x => new{ Data = x,Split = x.Name.Split(new char[] { '-' }) })
.Where(x => x.Split.Length >= 2)
.GroupBy(x => x.Split[1].Trim());

或者第二个如果有第二个,否则第一个:

var result = data
.Select(x => new{ Data = x,Split = x.Name.Split(new char[] { '-' }) })
.GroupBy(x => x.Split.Length >= 2 ? x.Split[1].Trim() : x.Split[0].Trim());

Cannot create PoolableConnectionFactory (Invalid number format for port number)

Cannot create PoolableConnectionFactory (Invalid number format for port number)

今天启动项目时出现这个异常,后来发现连接数据url少了端口号

添加端口号修改成jdbc:oracle:thin:@127.0.0.1:1521:orcl就好了

今天关于Countable Rational Numbers的分享就到这里,希望大家有所收获,若想了解更多关于315. Count of Smaller Numbers After Self、c# – 如何使用Reflection在IEnumerable上调用System.Linq.Enumerable.Count <>?、C#IEnumerable.Count()抛出IndexOutOfRangeException、Cannot create PoolableConnectionFactory (Invalid number format for port number)等相关知识,可以在本站进行查询。

本文标签: