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;
}

猜你喜欢

转载自blog.csdn.net/qq_41998938/article/details/83587493