SCU 4437 - Carries (进位)

## 题意 ##

问产生了多少次进位。

## 思路 ##

比赛的时候一直想单独处理每一位,但是想不出进位要怎么处理。

两个数a, b mod base,如果在这一位上他们有进位,a mod base + b mod base >= base。

用二分找一下就行。

## 代码 ##

#include <stack>
#include <cstdio>
#include <list>
#include <cassert>
#include <set>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#pragma comment(linker, "/STACK:102400000,102400000")
#include <string>
#include <map>
#include <cmath>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(p, num) memset(p, num, sizeof(p))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid+1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define FOR(i, a, b) for (int i=(a); (i) < (b); (i)++)
#define FOOR(i, a, b) for (int i = (a); (i)<=(b); (i)++)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 1e5+10;
const int MOD = 1e9+7;
const int dir[][2] = { {1, 0}, {0, -1}, {-1, 0}, {0, 1} };
const int seed = 131;
int cases = 0;
typedef pair<int, int> pii;
 
int arr[MAXN];
vector<int> tmp;
 
int main()
{
    //ROP;
    int n;
    while (~scanf("%d", &n))
    {
        for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
        LL mod = 1, ans = 0;
        for (int i = 0; i < 10; i++)
        {
            mod *= 10; tmp.clear();
            for (int j = 0; j < n; j++) tmp.push_back(arr[j] % mod);
            sort(tmp.begin(), tmp.end());
            for (int i = 0; i < n; i++)
            {
                if (tmp[i]+tmp[n-1] < mod) continue;
                int pos = lower_bound(tmp.begin(), tmp.end(), mod-tmp[i])-tmp.begin();
                ans += n-pos;
                if (pos <= i) ans--;
            }
        }
        printf("%lld\n", ans/2);
    }
    return 0;
}

Powered by Jekyll and Theme by solid