首页 > 代码库 > Codeforces 475 D.CGCDSSQ

Codeforces 475 D.CGCDSSQ

题目说了a的范围小于10^9次方,可实际却有超过的数据。。。真是醉了

算出以f[i]结尾的所有可能GCD值,并统计;

f[i]可以由f[i-1]得出.

/*       递推算出所有GCD值,map统计*/#include <iostream>#include <vector>#include <map>using namespace std;#define ll long longconst int MAXN = 100009;int n, m;ll x;map<ll , ll > sum, record[2];map<ll, ll>::iterator it;ll gcd (ll a, ll b) {    return b == 0 ? a : gcd (b, a % b);}int main() {    ios::sync_with_stdio (0);    cin >> n;    int roll = 1;    for (int i = 1; i <= n; i++, roll ^= 1) {        cin >> x;        record[roll].clear();        record[roll][x]++, sum[x]++;        for (it = record[roll ^ 1].begin(); it != record[roll ^ 1].end(); ++it) {            ll tem = gcd (x, (*it).first);            sum[tem] += (*it).second;            record[roll][tem] += (*it).second;        }    }    cin >> m;    for (int i = 1; i <= m; i++) {        cin >> x;        cout << sum[x] << endl;    }}
View Code

 

Codeforces 475 D.CGCDSSQ