結果

問題 No.1200 お菓子配り-3
ユーザー chocoruskchocorusk
提出日時 2020-08-29 03:48:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 297 ms / 4,000 ms
コード長 2,772 bytes
コンパイル時間 1,542 ms
コンパイル使用メモリ 142,728 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-05-01 00:03:38
合計ジャッジ時間 3,970 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 3 ms
6,944 KB
testcase_12 AC 12 ms
6,940 KB
testcase_13 AC 12 ms
6,944 KB
testcase_14 AC 11 ms
6,940 KB
testcase_15 AC 12 ms
6,940 KB
testcase_16 AC 12 ms
6,940 KB
testcase_17 AC 38 ms
6,940 KB
testcase_18 AC 52 ms
6,948 KB
testcase_19 AC 13 ms
6,944 KB
testcase_20 AC 103 ms
6,944 KB
testcase_21 AC 108 ms
6,940 KB
testcase_22 AC 119 ms
6,944 KB
testcase_23 AC 98 ms
6,944 KB
testcase_24 AC 98 ms
6,944 KB
testcase_25 AC 94 ms
6,944 KB
testcase_26 AC 97 ms
6,940 KB
testcase_27 AC 1 ms
6,940 KB
testcase_28 AC 297 ms
6,944 KB
testcase_29 AC 115 ms
6,940 KB
testcase_30 AC 113 ms
6,940 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<ll, int> P;
ll powmod(__int128_t a, ll k, ll m){
	__int128_t ap=a, ans=1;
	while(k){
		if(k&1){
			ans*=ap;
			ans%=m;
		}
		k>>=1;
		ap=ap*ap%m;
	}
	return ans;
}
bool is_prime(ll n){
	if(n<=1 || !(n&1)) return (n==2);
	ll d=n-1;
	int k=0;
	while(!(d&1)){
		d>>=1;
		k++;
	}
	vector<ll> va{2, 325, 9375, 28178, 450775, 9780504, 1795265022}; // http://miller-rabin.appspot.com/
	for(auto aa:va){
		ll a=aa%n;
		if(a==0) continue;
		bool comp=1;
		ll ap=powmod(a, d, n);
		if(ap==1 || ap==n-1) continue;
		for(int r=1; r<k; r++){
			ap=(__int128_t)ap*ap%n;
			if(ap==n-1){
				comp=0;
				break;
			}
		}
		if(comp) return false;
	}
	return true;
}
ll gcd(ll a, ll b){
	if(a==0) return b;
	if(b==0) return a;
	if(a<0) a=-a;
	if(b<0) b=-b;
	const int s=__builtin_ctzll(a|b);
	a>>=__builtin_ctzll(a);
	while(b){
		b>>=__builtin_ctzll(b);
		if(a>b) swap(a, b);
		b-=a;
	}
	return a<<s;
}
ll rho(ll n, ll& c){
	if(!(n&1)) return 2;
	while(1){
		ll x=2, y=2, d=1;
		while(d==1){
			x=((__int128_t)x*x+c)%n;
			y=((__int128_t)y*y%n+c)%n;
			y=((__int128_t)y*y%n+c)%n;
			d=gcd(abs(x-y), n);
		}
		if(d==n){
			c++;
			continue;
		}
		return d;
	}
}
vector<pair<ll, int>> factorize(ll n){
	if(n<=1){
		vector<pair<ll, int>> ret;
		return ret;
	}
	map<ll, int> mp;
	vector<ll> v(1, n);
	ll c=1;
	while(!v.empty()){
		ll x=v.back();
		v.pop_back();
		if(is_prime(x)){
			mp[x]++;
			continue;
		}
		ll p=rho(x, c);
		v.push_back(p);
		v.push_back(x/p);
	}
	vector<pair<ll, int>> ret;
	for(auto p:mp) ret.push_back(p);
	return ret;
}
ll calc(ll a, ll s, ll x, ll y){
    if(a==0) return 0;
    if(a==1){
        if(x==y) return s-1;
        else return 0;
    }
    if(x<=s) return 0;
    if((x-s)%(a-1)==0 && (x-s)/(a-1)<s) return 1;
    else return 0;
}
void dfs(int k, ll d, ll x, ll y, vector<P> &v, ll &ans){
    if(k==v.size()){
        ans+=calc(d-1, (x+y)/d, x, y);
        return;
    }
    ll q=1;
    for(int i=0; i<=v[k].second; i++){
        dfs(k+1, d*q, x, y, v, ans);
        q*=v[k].first;
    }
}
ll solve(ll x, ll y){
    ll ans=0;
    auto v=factorize(x+y);
    dfs(0, 1, x, y, v, ans);
    return ans;
}
int main()
{
    int s; cin>>s;
    while(s--){
        ll x, y; cin>>x>>y;
        cout<<solve(x, y)<<endl;
    }
    return 0;
}
0