nyoj-686-An Interesting Sequence【分解质因数】

来源:互联网 发布:2017省市区数据库sql 编辑:程序博客网 时间:2024/06/05 16:15

nyoj-686-An Interesting Sequence


描述
YiYi is a smart boy, he is fascinated in solving eccentric problems. One day when he was reading a book, he came across a very interesting problem. After a few time’s thinking, he finally get the solution. YiYi considers this solution very heuristic, now he decides to show this problem to you so that you can also enjoy the happiness of solving this interesting problem. Here is the description of the problem:

Suppose at first you have a sequence, S1, consists of two numbers: 1, 1. According to some rule you can get the sequence Sn from the sequence Sn-1.Once you get Sn-1, you will be asked to insert one n between a and b if a and b are two adjacent numbers in Sn-1 and a + b = n. After you have inserted all possible n you finally get Sn. You can also get the sequence Sn+1 using the same method and so on.

For example,S1 = 1,1.Since 1 + 1 = 2, we will insert 2 between two 1s and then we get S2, S2 = 1,2,1. Now we will insert two 3s in the sequence to get S3, as you can see, S3 = 1,3,2,3,1. In the same way, we know S4 = 1,4,3,2,3,4,1, S5 = 1,5,4,3,5,2,5,3,4,5,1 and so on.

Here the question comes: give you n, you will be asked to output the number of n in Sn. For example if n = 5, you will output 4 since there are 4 5s in S5.

输入
The first line of the input contains a number k, the number of test cases to solve (1 ≤ k ≤ 200). Each test case consists of a single integer 2 ≤ n ≤ 10^10 on a separate line.
输出
For each test case, you are asked to output the number of n in Sn on a line.

样例输入
3
2
4
5
样例输出
1
2
4

题目链接:nyoj-686

题目大意:如果si 和 si+1 相加等于n,则在这两个数中间插入n,问为n的时候能插入多少个数字

题目思路:先暴力出前面一部分数字可以发现规律。即分解质因数。

以下是代码:

#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <cstring>#include <algorithm>#include <string>#include <set>#include <functional>#include <numeric>#include <sstream>#include <stack>#include <map>#include <queue>#include<iomanip>using namespace std;#define ll long longll prm[100000];const int MAXV = 1e5+600; bool isPrime[MAXV+1]; ll size=0; ll qpow(ll a, ll n ) {ll ret = 1;while (n) {    if (n & 1) ret = ret * a;        a = a * a;    n>>= 1;    }return ret;}void getPrime()  {      memset(isPrime, true, sizeof(isPrime));    ll sq = sqrt((double)MAXV) + 1;     ll i,j,k;      for(i = 2;i <= sq; i++)          if(isPrime[i])      for(j = 2,k = MAXV/i+1;j < k;j++)          isPrime[i*j] = false;      for( i = 2 ; i <= MAXV; i++)          if(isPrime[i])                prm[size++] = i;    isPrime[0] = isPrime[1] = false;}  int main(){    getPrime();    int t;    cin >> t;    while( t-- ){        ll a;        ll ans = 1;        cin >> a;        ll s = a;        for( int i=0 ; i<100000 ; i++ ){            if( prm[i]>100000||prm[i]>a ) break;            ll cnt = 0;            while( a%prm[i]==0 ){                cnt++;                a/=prm[i];            }            if (cnt >= 1) ans *= (prm[i] - 1) * qpow(prm[i],(ll)cnt - 1);        }        if( a!=1 ) cout << ans*(a-1) << endl;        else cout << ans << endl;    }    return 0;}
0 0
原创粉丝点击