Java練習 SDUT-2499_數字
阿新 • • 發佈:2018-11-01
數字
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
定義f(x) = {比x小,不可以被x整除並且不和x互質的數的個數}(x為正整數)。
當f(x) 是奇數的時候我們稱x為“奇真數”。
給出兩個數x,y求區間[x,y]內的“奇真數”的個數。
Input
第一行輸入一個數N代表測試資料個數(N<=20)。接下來N行每行兩個正整數x , y ( 0 < x <= y < 2^31)。
Output
對於每個測試資料輸出“奇真數”的個數,每行輸出一個結果。
Sample Input
2
1 1
1 10
Sample Output
0
4
Hint
中國海洋大學第三屆“朗訊杯”程式設計比賽高階組試題
詳細題解:泳褲王子的部落格-HDU 4279 Number [數論+簡證]
import java.util.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); long a,b,t; t = cin.nextLong(); while(t-->0) { a = cin.nextLong(); b = cin.nextLong(); System.out.println(f(b) - f(a - 1)); } cin.close(); } static long f(long a) { if(a<=2) return 0; if((long)Math.sqrt(a)%2==1) return a / 2 - 1; else return a / 2 - 2; } }