【Java/补题/牛客/ACM赛制】2021年ICPC国际大学生程序设计竞赛暨陕西省第九届大学生程序设计竞赛(正式赛)

题目链接

2021年ICPC国际大学生程序设计竞赛暨陕西省第九届大学生程序设计竞赛(正式赛)

知识一览

01-数论分块

题目列表

快输

    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        String tmp;

        public FastReader() {
            br=new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while(st==null||!st.hasMoreElements()) {
                try {
                    st=new StringTokenizer(br.readLine());
                }catch(IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong(){return Long.parseLong(next());}

        String nextLine() {
            String str="";
            try {
                str=br.readLine();
            }catch(IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        boolean hasNext(){
            if(st!=null&&st.hasMoreTokens())return true;
            try {
                tmp=br.readLine();
                st=new StringTokenizer(tmp);
            }catch(IOException e) {
                return false;
            }
            return true;
        }
    }

    static PrintWriter out=new PrintWriter(
            new BufferedWriter(new OutputStreamWriter(System.out)));
    static FastReader sc=new FastReader();

C - GCD(数论分块)

题目描述
Bob is interested in GCDs (Greatest Common Divisors). Formally, for some positive integers (a1,…,ak), we denote gcd (a1,…,ak) as the greatest positive integer dd, such that d|ai for each 1≤i≤k.
Bob has chosen an interval [l,r]. He is going to choose kk distinct integers from the interval and compute their GCD. There are many integers that can be the final answer, and Bob is curious in the number of such integers. Please write a program to tell him the answer.
输入描述:
The first and only line of input consists of three integers l, r, kl,r,k(1≤l≤r≤10^12
,2≤k≤r−l+1).
输出描述:
Print a single integer, indicating your answer.
示例1
输入
5 9 2
输出
3
说明
For the sample, it is possible to get 1,2,3 as the final GCD, by choosing numbers (7,9),(6,8) and (6,9). It turns out that any other integer cannot become the GCD, thus the answer is 3.

答案参考:C.GCD(整除分块优化)

关于数论分块
首先明确:n % k = n - n / k * k
n/k会在某一连续区间内保持不变,如20 / 11 = 1 , 20 / 12 = 1等等
n / k 在哪一段连续区间都相等呢,这就是一个结论了 : [l,n / (n / l)]

	public static void main(String[] args) {
		long L=sc.nextLong(),R=sc.nextLong(),k=sc.nextLong();
	    L--;
	    long l,r,ans=0;
	    for(long i=1;i<=R;i=r+1){
	        l=i;
	        //分块时L/i与R/i的左边界可能不同,所以要取较小的右边界。
	        if(i<=L)r=Math.min(L/(L/i),R/(R/i));
	        else r=R/(R/i);//L/i(i>L)所以恒为0,只要考虑R/i的右边界。
	        if((R/i-L/i)>=k)ans+=r-l+1;
	    }
	    out.println(ans);
        out.flush();
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值