HDU6124-Euler theorem
HDU6124-Euler theorem
題目:
HazelFan is given two positive integers
a,b
a,b
, and he wants to calculate
amodb
amodb
. But now he forgets the value of
b
b
and only remember the value of
a
a
, please tell him the number of different possible results.
Input
The first line contains a positive integer
T(1≤T≤5)
T(1≤T≤5)
, denoting the number of test cases.
For each test case:
A single line contains a positive integer
a(1≤a≤
10
9
)
a(1≤a≤109)
.
Output
For each test case:
A single line contains a nonnegative integer, denoting the answer.
Sample Input
2
1
3
Sample Output
2
3
剛看到這道題,想用陣列標記的方法去完成,但是超時了。
超時程式碼如下:
#include<bits/stdc++.h> using namespace std; #define MAX 100005 long long b[MAX]; int main() { long long a,count; int T; scanf("%d",&T); while(T--) { scanf("%lld",&a); count = 0; memset(b,0,a + 1); for(int i = 1;i <= a + 1;i++) b[a % i] = 1; for(int i = 0;i <= a + 1;i++) { if(b[i] == 1) count++; } cout << count << endl; } return 0; }
後來通過幾個數試驗之後,發現其實這道題是一道找規律題,下面來舉幾個數的例子:
6 餘數有6,0,1,2。總共4個
8 餘數有8,0,2,3,1。總共5個
13 餘數有13,0,1,3,6,5,4,2,總共8個
…
可以列舉很多例子,發現所有數字餘數的個數都滿足一個規律,如果這個數是a,那麼這個數餘數的個數就是(a + 1)/ 2 + 1。果然程式碼ac了
ac程式碼如下:
#include<bits/stdc++.h> using namespace std; int main() { int T,a; scanf("%d",&T); while(T--) { scanf("%d",&a); printf("%d\n",(a + 1) / 2 + 1); } return 0; }