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

2017CCPC秦皇岛

时间:2017-11-12 12:26:33      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:mat   cal   using   push   lang   anyway   less   nsis   ice   

热身赛

D题 17171771:DFS + Miller_Rabin

Time Limit: 2 Seconds Memory Limit: 65536 KB 17171771 is a sweet 
song in Jaurim’s 5th album, “All You Need Is Love”, released in 
October 2004.

What’s the meaning of 17171771? If we rotate it by 180 degrees, it 
looks like “ILLILILI”. If we add some blanks into it, it becomes “I 
LLILI LI”. Doesn’t it look like “I LUV U”? The meaning of 17171771 is 
“I LUV U”. Anyway, it has nothing to do with our problem.

What we are concerned more about is that, 17171771 is a prime 
consisting only of digits 1 and 7 occurring with equal frequency. In 
this problem, a prime consisting only of two different digits 
occurring with equal frequency is called nice number. For example, 89, 
71717117 and 23323333222223 are nice numbers.

Your task is to print all the nice numbers which are strictly less 
than 1018

Input

There is no input for this problem.

Output

Output all the nice number less than 1018 in increasing order. The 
output looks like the following:

13 17 19 … 17171771

AC代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
typedef long long ll;
using namespace std;

vector <ll> vi;
set <ll> st;

void dfs(int *a,int len)
{
    do{
        ll tp = 0;
        if(a[0] == 0) continue;
        for(int i=0;i<len;i++)
            tp = tp * 10 + a[i];
        vi.push_back(tp);
    }while(next_permutation(a, a + len));
}

ll prime[6] = {2, 3, 5, 233, 331};
ll qmul(ll x, ll y, ll mod) // 乘法防止溢出, 如果p * p不爆ll的话可以直接乘; O(1)乘法或者转化成二进制加法
{
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}
ll qpow(ll a, ll n, ll mod)
{
    ll ret = 1;
    while(n) {
        if(n & 1) ret = qmul(ret, a, mod);
        a = qmul(a, a, mod);
        n >>= 1;
    }
    return ret;
}
bool Miller_Rabin(ll p)
{
    if(p < 2) return 0;
    if(p != 2 && p % 2 == 0) return 0;
    ll s = p - 1;
    while(! (s & 1)) s >>= 1;
    for(int i = 0; i < 5; ++i)
    {
        if(p == prime[i]) return 1;
        ll t = s, m = qpow(prime[i], s, p);
        while(t != p - 1 && m != 1 && m != p - 1) {
            m = qmul(m, m, p);
            t <<= 1;
        }
        if(m != p - 1 && !(t & 1)) return 0;
    }
    return 1;
}

int main()
{
    int a[20] = {0};
    for(int i=0;i<=9;i++)
        for(int j=i+1;j<=9;j++)
            for(int k=1;k<=8;k++)
            {
                for(int l=0;l<k;l++)
                {
                    a[l] = i;
                    a[l+k] = j;
                }
                dfs(a, k*2);
            }
    for(int i=0;i<vi.size();i++)
        if(Miller_Rabin(vi[i]))
            st.insert(vi[i]);

    int cnt = 0;
    for(set<ll>::iterator it = st.begin(); it != st.end(); it++)
    {
        printf("%lld\n",(*it));
        cnt ++;
    }
    cout << cnt << endl;
}

 

 

2017CCPC秦皇岛

标签:mat   cal   using   push   lang   anyway   less   nsis   ice   

原文地址:http://www.cnblogs.com/HazelNut/p/7821078.html

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