码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 475 D.CGCDSSQ

时间:2014-10-09 00:19:47      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   数据   

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

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

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

bubuko.com,布布扣
/*
       递推算出所有GCD值,map统计
*/
#include <iostream>
#include <vector>
#include <map>
using namespace std;
#define ll long long
const 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

标签:style   blog   http   color   io   os   ar   for   数据   

原文地址:http://www.cnblogs.com/keam37/p/4011551.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!